외부 Postgres 인스턴스로 마이그레이션 시 오류 발생

기본 자체 포함(self-contained) 구성으로 discourse-docker를 사용하여 포럼을 설정했습니다. 이제 별도의 postgres 서버를 사용하도록 전환해야 합니다.

백업을 수행한 후 app.yml을 수정하고 다시 빌드했으며, CLI를 통해 백업에서 복원하려고 시도했습니다. 복원이 실패했고, postgres에 접근하기 위해 올바른 호스트를 사용했지만, 설정한 포트(9432)가 아닌 기본 포트(5432)를 사용하고 있는 것으로 보입니다.

오류:

psql: error: connection to server at "[REDACTED HOST]" ([REDACTED IP]), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
EXCEPTION: psql failed: 	Is the server running on that host and accepting TCP/IP connections?
/var/www/discourse/lib/backup_restore/database_restorer.rb:92:in `restore_dump'
/var/www/discourse/lib/backup_restore/database_restorer.rb:26:in `restore'
/var/www/discourse/lib/backup_restore/restorer.rb:51:in `run'
script/discourse:149:in `restore'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/3.2.0/gems/thor-1.3.0/lib/thor/base.rb:584:in `start'
script/discourse:290:in `<top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:58:in `kernel_load'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli/exec.rb:23:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:451:in `exec'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:34:in `dispatch'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/cli.rb:28:in `start'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:28:in `block in <top (required)>'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/usr/local/lib/ruby/gems/3.2.0/gems/bundler-2.5.3/exe/bundle:20:in `<top (required)>'
/usr/local/bin/bundle:25:in `load'
/usr/local/bin/bundle:25:in `<main>'
Trying to rollback...
Rolling back...
Cleaning stuff up...
Dropping functions from the discourse_functions schema...
Removing tmp '/var/www/discourse/tmp/restores/default/2024-01-17-201724' directory...
Unpausing sidekiq...
Marking restore as finished...
Notifying 'system' of the end of the restore...
Finished!
[FAILED]

설정:
app.yml의 templates: 섹션에서 - "templates/postgres.template.yml" 주석을 처리했습니다.

또한 env 아래에 다음을 추가했습니다:

  DISCOURSE_DB_USERNAME: [REDACTED]
  DISCOURSE_DB_PASSWORD: [REDACTED]
  DISCOURSE_DB_HOST:  [REDACTED]
  DISCOURSE_DB_PORT: 9432
  DISCOURSE_DB_NAME: [REDACTED]

다시 빌드했을 때 새로운 빈 사이트가 생성되었나요? 그렇지 않다면 그 부분에 문제가 있는 것입니다.

컨테이너 내부에서 psql <데이터베이스를 가리키는 몇 가지 옵션>을 실행하여 데이터베이스에 접근할 수 있나요?

컨테이너 내부에서 set|grep DB를 실행하면 ENV 변수가 보입니까?

세 가지 모두 그렇습니다. 올바른 데이터베이스에 연결된 새 빈 사이트가 생성됩니다. 환경 변수는 grep으로 검색할 때 설정되어 있고, 컨테이너 내에서 수동으로 데이터베이스에 연결할 수도 있습니다.

복원 스크립트가 ENV 변수를 무시하는 반면 다른 모든 요소는 정상적으로 동작하는, 매우 희소한 가능성이 있습니다. 이를 확인하려면 코드를 검토하거나 UX에서 복원을 시도해 보는 방법이 있습니다. UX에서 복원이 성공한다면, 이 이슈를 #contribute:bug로 옮기는 것이 적절할 것입니다.

UX를 통해 복구를 시도해 보았지만, 동일한 오류가 발생했습니다.

그럼 다음으로 할 일은 코드를 살펴보는 것일 거예요.

특히 다른 모든 것에서는 정상적으로 작동한다는 점이 이상합니다.

rails 콘솔에 진입하여 다음 값들을 보여줄 수 있을까요?

BackupRestore.database_configuration
BackupRestore::DatabaseRestorer.psql_command

예시:

→ rails c
Loading development environment (Rails 7.0.8)
[1] pry(main)> BackupRestore.database_configuration
=> #<struct BackupRestore::DatabaseConfiguration
 host=nil,
 port=nil,
 username="michael",
 password=nil,
 database="discourse_development">

[2] pry(main)> BackupRestore::DatabaseRestorer.psql_command
=> "psql --dbname='discourse_development' --single-transaction --variable=ON_ERROR_STOP=1 --username=michael"

요청하신 내용입니다! 제가 편집한 부분들은 모두 정확합니다. 포트만 잘못된 것입니다.

[1] pry(main)> BackupRestore.database_configuration
=> #<struct BackupRestore::DatabaseConfiguration
 host="[REDACTED[",
 port=5432,
 username="[REDACTED]",
 password="[REDACTED]",
 database="[REDACTED]">
[2] pry(main)> BackupRestore::DatabaseRestorer.psql_command
=> "PGPASSWORD='[REDACTED]' psql --dbname='[REDACTED]' --single-transaction --variable=ON_ERROR_STOP=1 --host=[REDCATED] --port=5432 --username=[REDACTED]"

또한 빈 인스턴스가 백업에서 복원하는 것을 제외하고는 데이터베이스에 연결할 수 있다는 점을 다시 강조합니다.

다음은 무엇인가요?

ActiveRecord::Base.connection_pool.db_config.configuration_hash
[3] pry(main)> ActiveRecord::Base.connection_pool.db_config.configuration_hash
=> {:adapter=>"postgresql",
 :pool=>8,
 :connect_timeout=>5,
 :socket=>"/var/run/postgresql",
 :host=>"[REDACTED]",
 :port=>9432,
 :backup_port=>5432,
 :username=>"[REDACTED]",
 :password=>"[REDACTED]",
 :host_names=>["[REDACTED]"],
 :database=>"[REDACTED]",
 :prepared_statements=>false,
 :idle_timeout=>30,
 :reaping_frequency=>30,
 :advisory_locks=>true}

아! 또한 다음을 설정해야 합니다

DISCOURSE_DB_BACKUP_PORT: 9432

그리고 나서 모든 것이 정상적으로 작동할 것입니다.

수정: 이 기본값은 이미 오래전부터 적용되어 왔지만, 도움이 되기보다 더 많은 어려움을 야기한 것 같습니다.

FIX: clear db_backup_port default value - Pull Request #25316 - discourse/discourse - GitHub 도 이 문제를 해결하고, 향후 방문자들이 같은 혼란을 겪지 않도록 도와줄 것입니다

정말 감사합니다! 감사합니다!