# PG12에서 db:migrate 실행 시 Rebuild 실패

**URL:** https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287
**Category:** Self-hosting
**Created:** [4월 14, 2022, 3:25오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287 "2022-04-14T15:25:57Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 14, 2022, 3:25오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/1 "2022-04-14T15:25:57Z")

</div>

“./launcher rebuild app” 실행 시 db:migrate에서 실패합니다. 참고로 PostgreSQL v12를 사용하고 있습니다.

이 문제로 인해 포럼이 망가졌습니다. Docker 컨테이너는 다시 시작되었지만, 포럼은 작동하지 않았습니다. 다행히 업그레이드 전에 VM 스냅샷을 찍어두었기 때문에 지금 이를 복원하고 있습니다.

로그:

```plaintext
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I, [2022-04-14T15:20:51.896917 #1] INFO -- : == 20220304162250 EnableUnaccentExtension: migrating ==========================
-- enable_extension("unaccent")

I, [2022-04-14T15:20:51.897218 #1] INFO -- : Terminating async processes
I, [2022-04-14T15:20:51.897265 #1] INFO -- : Sending INT to HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/12/bin/postmaster -D /etc/postgresql/12/main pid: 1710
I, [2022-04-14T15:20:51.897396 #1] INFO -- : Sending TERM to exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf pid: 1827
2022-04-14 15:20:51.897 UTC [1710] LOG: received fast shutdown request
1827:signal-handler (1649949651) Received SIGTERM scheduling shutdown...
2022-04-14 15:20:51.900 UTC [1710] LOG: aborting any active transactions
2022-04-14 15:20:51.902 UTC [1710] LOG: background worker "logical replication launcher" (PID 1719) exited with exit code 1
2022-04-14 15:20:51.904 UTC [1714] LOG: shutting down
1827:M 14 Apr 2022 15:20:51.913 # User requested shutdown...
1827:M 14 Apr 2022 15:20:51.914 * Saving the final RDB snapshot before exiting.
2022-04-14 15:20:51.965 UTC [1710] LOG: database system is shut down
1827:M 14 Apr 2022 15:20:53.157 * DB saved on disk
1827:M 14 Apr 2022 15:20:53.157 # Redis is now ready to exit, bye bye...

FAILED
--------------------
Pups::ExecError: cd /var/www/discourse && su discourse -c 'bundle exec rake db:migrate' failed with return #<Process::Status: pid 2118 exit 1>
Location of failure: /usr/local/lib/ruby/gems/2.7.0/gems/pups-1.1.1/lib/pups/exec_command.rb:117:in `spawn'
exec failed with the params {"cd"=>"$home", "hook"=>"db_migrate", "cmd"=>["su discourse -c 'bundle exec rake db:migrate'"]}
bootstrap failed with exit code 1
**FAILED TO BOOTSTRAP** please scroll up and look for earlier error messages, there may be more than one.
./discourse-doctor may help diagnose the problem.
2dcd9aeca614c9e06ef748f673eb68203db6eae5c445253b416d666663879d6d
==================== END REBUILD LOG ====================
Failed to rebuild app.

```

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 14, 2022, 4:18오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/5 "2022-04-14T16:18:41Z")

</div>

별개의 구성이 아니며 외부 PG도 없습니다. PG13 업그레이드도 실패합니다(오늘의 업그레이드와 달리 파괴적이지 않음). 솔직히 여기에서 이를 수정하는 방법에 대한 지원을 받지 못했습니다.

(물론 확인할 수는 없지만) docker ps에 표시된 컨테이너가 하나뿐이었을 것 같습니다. 표준 설치 방식은 이제 2개의 컨테이너인가요?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [4월 14, 2022, 5:09오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/11 "2022-04-14T17:09:05Z")

</div>

> [@Wingtip](#):
>
> ` enable_extension("unaccent")`

이 확장 프로그램은 PostgreSQL 13 이상에서 “신뢰할 수 있는”(trusted) 확장으로 사용 가능하며, 이 경우 모든 사용자가 활성화할 수 있습니다.

현재 구버전의 PostgreSQL을 사용 중이므로, Discourse 사용자를 위해 이 확장 프로그램을 설치하고 활성화하는 우회 방법을 사용해야 합니다. 또한 Discourse가 이 확장 프로그램이 이미 설치된 것으로 인식하도록 속이는 방법을 시도해 볼 수도 있습니다. 아니면 현재 지원되는 PostgreSQL 버전으로 업그레이드하는 것이 좋습니다.

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 14, 2022, 5:15오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/12 "2022-04-14T17:15:36Z")

</div>

알겠습니다. 요약하자면, 더 이상 PG12를 지원하지 않는다는 것이군요. PG13 업그레이드 스레드 어딘가에, 그리고 아마 2.9.0b4 공지사항에도 그 내용을 게시하는 것이 좋겠습니다.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [4월 14, 2022, 7:17오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/13 "2022-04-14T19:17:57Z")

</div>

다운타임이 걱정된다면, 서버를 새로운 호스트로 복제하는 것이 하나의 해결책이 될 수 있습니다. [community/archived/setting-up-postgres-hot-standby.md at master · GoogleCloudPlatform/community · GitHub](https://cloud.google.com/community/tutorials/setting-up-postgres-hot-standby) 와 같은 방법을 참고할 수 있습니다. (더 최신인 설명서나, 본인에게 더 잘 맞는 설명서를 찾을 수도 있을 것입니다…) 이렇게 하면 기존 데이터베이스가 계속 작동하는 동안 새 데이터베이스로 마이그레이션을 수행할 수 있습니다. 하지만 이 방법은 결코 간단하지 않습니다.

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 14, 2022, 7:20오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/14 "2022-04-14T19:20:45Z")

</div>

좋은 아이디어네요. MySQL, MS-SQL, Oracle였다면 그렇게 했을 텐데, Postgres에 대한 경험이 부족해서 그냥 다운타임을 감수할 것 같아요.

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 14, 2022, 7:50오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/15 "2022-04-14T19:50:33Z")

</div>

드디어 4시간이 넘게 걸린 복원이 끝났습니다. 그런데 Discourse에서 502 에러가 계속 발생하고 있네요. 이 스냅샷은 업그레이드 _이전_에 찍은 것이기 때문에 정말 기가 막힙니다.

일단 nginx 로그를 살펴보니 이런 에러를 발견했습니다.

```plaintext
2022/04/14 19:36:21 [error] 493#493: *350 connect() failed (111: Connection refused) while connecting to upstream, client: 216.228.112.21, server: _, request: "POST /message-bus/15f7a893581d489e930634c8f3ed1134/poll?dlp=t HTTP/2.0", upstream: "http://127.0.0.1:3000/message-bus/15f7a893581d489e930634c8f3ed1134/poll?dlp=t", host: "forum.quartertothree.com", referrer: "https://forum.quartertothree.com/c/movies/8"

```

그리고 ruby 로그에는 다음과 같은 내용이 있었습니다.

```plaintext
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.10.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require': cannot load such file -- /var/www/discourse/lib/freedom_patches/schema_cache_concurrency.rb (LoadError)

```

정말 그랬네요. 해당 파일은 root:root 소유였고 권한이 0000이었습니다. 해당 디렉토리의 다른 파일들과 동일하게 discourse:root 소유, 644 권한으로 변경하니 다시 정상 작동했습니다. 후~!

어떻게 해서 그 파일이 삭제되거나 변경된 건지 혹시 아시는 분 계신가요? 파일 크기도 0바이트라 정말 이상합니다.

```plaintext

root@forum-app:/shared/log/rails# ls -la /var/www/discourse/lib/freedom_patches/schema_cache_concurrency.rb
-rw-r--r-- 1 discourse root 0 Feb 10 17:41 /var/www/discourse/lib/freedom_patches/schema_cache_concurrency.rb

```

---

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [4월 30, 2022, 1:40오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/16 "2022-04-30T01:40:14Z")

</div>

> [@Wingtip](#):
>
> OK. 그러니까 요약하면, 더 이상 PG12을 지원하지 않는다는 거군요.

이거 ☝

이제 다시 "업그레이드에 300GB가 필요해서 공간이 부족하다"는 문제를 마주하게 되었습니다.

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [4월 30, 2022, 1:56오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/17 "2022-04-30T01:56:16Z")

</div>

행운을 빕니다. 제가 볼 수 있는 한, 새 호스트에 백업을 복원하는 것 외에는 실질적인 해결책이 없습니다.

---

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [5월 8, 2022, 7:59오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/18 "2022-05-08T07:59:47Z")

</div>

감사합니다만, 사실상 죽은 상태입니다.

"새 설치로 복원"을 시도해 봤지만, PG가 협조하지 않아서 작동하지 않았습니다. Discourse 버전을 낮추고 PG12로 돌아가려 했지만, 그러면 모든 플러그인 때문에 아수라장이 됩니다.

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [5월 8, 2022, 8:27오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/19 "2022-05-08T08:27:52Z")

</div>

기본 템플릿 대신 이 postgres 템플릿으로 재빌드를 시도해 보셨나요?

> [discourse\_docker/templates/postgres.12.template.yml at main · discourse/discourse\_docker · GitHub](https://github.com/discourse/discourse_docker/blob/main/templates/postgres.12.template.yml)

---

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [5월 8, 2022, 8:35오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/20 "2022-05-08T08:35:32Z")

</div>

우선 도움을 주셔서 감사합니다.

네, "이건 안 되네"라는 전략의 일환으로 템플릿을 변경했기 때문에 PG12로 되돌아갈 수 있었습니다(물론 이렇게 하면 나중에 PG를 어떻게 업그레이드해야 할지 의문이 듭니다 🤔).

특정 커밋을 찾아야 했지만, 이 커밋은 안전한 선택인 것 같습니다: [Version bump to v2.8.0.beta10 (#15382) · discourse/discourse@07c0104 · GitHub](https://github.com/discourse/discourse/commit/07c010422bf4e65f728ba7d73902de7f9df048ed)

더 최근의 커밋도 시도해 보았지만 `enable_extension("unaccent")` 오류가 여전히 존재합니다. 이는 해당 커밋들에서는 이미 unaccent 확장 의존성을 변경한 상태라는 것을 의미합니다.

이번 시도의 결과를 기다리는 중입니다.

업데이트: 아니요, “덤프 언패킹” 단계에서 복원 중 실패했고, 지금은 다시 죽었습니다.

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [5월 8, 2022, 9:11오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/21 "2022-05-08T09:11:06Z")

</div>

안녕하세요. Discourse 백업이 있다면, 먼저 다른 서버에서 테스트해 보시는 것을 권장합니다.

구버전의 Discourse 인스턴스를 업그레이드하는 과정에서 이 문제를 경험하신 것으로 보입니다.

따라서 yml 파일을 수동으로 편집하여 Discourse를 "stable"로 사용하고 Postgres 버전을 12로 고정하여 Discourse의 복사본을 설치해 보세요.

빌드가 성공하면 백업 복원을 시도해 보세요. Hopefully, it will restore successfully.  
성공하면 Postgres 12 템플릿을 기본 Postgres 템플릿으로 되돌리고 stable 태그를 주석 처리하여 Discourse가 최신 tests-passed 버전으로 다시 빌드되도록 하세요.

백업이 복구 가능한 상태라면, Postgres와 Discourse 업그레이드를 무사히 통과할 수 있을 것입니다.

문제가 발생하면 알려 주세요.

---

<div class="post-metadata">

### Author: ![Iceman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/iceman/32/181309_2.png) [@Iceman](https://meta.discourse.org/u/Iceman)
#### Post date: [5월 8, 2022, 10:31오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/22 "2022-05-08T10:31:20Z")

</div>

지금 저는 사실상 “리밍보(limbo)” 상태입니다. PG12와 “Stable” 버전으로 제안하신 방법을 시도해 보았지만, 작동하지 않으며 복원 과정이 그냥 멈춰 버립니다. 그래서 이제 앱 재빌드도 되지 않는 상황이 되어, 처음부터 다시 시도하기 위해 머신을 다시 초기화하고 있습니다.

해당 머신이 “PG12로 되돌아가는” 작업을 하는 동안, 다른 머신에서는 앞으로 나아갈 수 있는지 확인해 보고 있습니다. 새 설치로 PG를 업그레이드하려고 하면 `Creating missing functions in the discourse_functions schema...` 단계에서 죽어버리고(500 오류를 반환하기 시작하며), `tail -f shared/data/log/var-log/postgres/current`를 확인해 보면 데이터 컨테이너가 "동작 중"인 것처럼 보이지만, 실제로는 다음과 같은 "오류"로 가득 차 있습니다:

```plaintext
discourse@discourse ERROR: relation "user_auth_tokens" does not exist at character 34
discourse@discourse STATEMENT: SELECT "user_auth_tokens".* FROM "user_auth_tokens" WHERE ((auth_token = 'XXXX=' OR
                                  prev_auth_token = 'XXXX=') AND rotated_at > '2022-03-09 10:21:44.051357') LIMIT 1

```

```plaintext
discourse@discourse ERROR: relation "application_requests" does not exist at character 41
discourse@discourse STATEMENT: SELECT "application_requests"."id" FROM "application_requests" WHERE "application_requests"."date" = '2022-05-08' AND "application_requests"."req_type" = 0 LIMIT 1

```

하지만 Discourse는 죽어 있을지 몰라도 머신은 **사용 중** 이므로… 작동은 하고 있지만 그냥 몇 시간씩 걸리는 건가요? 1시간 넘게 방任해 두었는데 아무런 변화가 없었거든요.

이쯤 되면 이 문제가 [여기](https://meta.discourse.org/t/postgresql-13-update/172563)에 올려야 하는 건 아닌지 고민 중입니다, 하하.

P.S. 모험을 시작하기 전에 백업을 두 번 했기 때문에 서로 다른 백업 두 개를 시도해 보았습니다.

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [5월 8, 2022, 11:00오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/23 "2022-05-08T11:00:58Z")

</div>

> [@Iceman](#):
>
> 복원이 그냥 멈춰요.

‘그냥 멈춘다’는 게 정확히 무슨 뜻인지 이해가 되지 않습니다. 어떤 오류 메시지가 표시되나요? 화면이 얼어붙는 현상이 있나요? 아니면 다른 문제가 있나요?

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [5월 8, 2022, 11:13오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/24 "2022-05-08T11:13:20Z")

</div>

그건 좋지 않네요. PG12가 설치된 구버전 Discourse의 백업을 PG13이 설치된 새 인스턴스로 복원할 수 없었나요? 에러 메시지 등을 공유해 주실 수 있나요? 여기에서 여러 번 그 작업이 가능하다고 확신을 주셨는데 말이죠.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [5월 8, 2022, 4:38오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/25 "2022-05-08T16:38:29Z")

</div>

> [@Iceman](#):
>
> “새로 설치로 복원”을 하려는데 작동하지 않습니다. PG가 응답하지 않아요.

어떤 의미인가요? 오류가 발생하나요?

---

<div class="post-metadata">

### Author: ![Samir\_Sabri](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/samir_sabri/32/287380_2.png) [@Samir\_Sabri](https://meta.discourse.org/u/Samir_Sabri)
#### Post date: [12월 31, 2022, 7:05오전 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/26 "2022-12-31T07:05:59Z")

</div>

이렇게 문제를 해결했습니다:

```plaintext
CREATE EXTENSION unaccent;

```

PG를 업그레이드할 필요 없이요.

---

<div class="post-metadata">

### Author: ![Wingtip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wingtip/32/102039_2.png) [@Wingtip](https://meta.discourse.org/u/Wingtip)
#### Post date: [12월 31, 2022, 4:55오후 UTC](https://meta.discourse.org/t/rebuild-fails-on-db-migrate-w-pg12/224287/27 "2022-12-31T16:55:22Z")

</div>

한 해 전에 알았으면 좋았을 텐데, 그래도 고마워요!

여기 계신 분들을 위해 말씀드리자면, 결국 새 VM에 클린 설치로 전체 복원을 진행했고, 문제없이 잘 작동했습니다.
