DockerでPostgresを実行してDiscourseを実行する

こんにちは

ローカル開発環境でDiscourseをセットアップしようとしていますが、一点だけ違いがあります。PostgresをDockerで実行しています。
データベースにはOSからpsqlでアクセスできますが、rakeやrailsでデータベースを作成しようとするとうまくいきません。以下の手順に従いました。

cp discourse_defaults.conf discourse.conf

その後、discourse.confを更新しました。

...
db_host = 127.0.0.1
...
db_username = postgres
db_password = mysecretpassword
...

しかし、db:createを実行しようとすると、以下のエラーが発生します。

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?
Couldn't create 'discourse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?


Tasks: TOP => db:create
(See full trace by running task with --trace)

.envでも試しました。

DISCOURSE_DB_HOST=127.0.0.1
DISCOURSE_DB_USERNAME=postgres
DISCOURSE_DB_PASSWORD=mysecretpassword

DB_HOSTに異なるIPアドレスを試しましたが、同じエラーが発生します。

何か見落としていることはありますか?

確信はありませんが、問題はおそらく、ディスコースがDockerコンテナ内で実行されており、デフォルトでは別のPostgresコンテナにアクセスできないことだと思われます。

データベースをこの種の構成で公開する必要があると思われます。GitHub - discourse/discourse_docker: A Docker image for Discourse

app.ymlファイルからPostgresテンプレートを削除する必要もあるかもしれません。

これらのドキュメントは役立つかもしれません。Configure Discourse to use a separate PostgreSQL server - 別のサーバーで実行しているわけではありませんが、それ以外はすべて関連性があるはずです。

お役に立てば幸いです!

Hi @phil22

ご返信ありがとうございます。

DiscourseはDockerコンテナ内ではなく、私のOSで実行されています。Postgresはコンテナ内で実行されており、そのポートをOSに公開しました -p 5432:5432

これは、Discourse がファイルシステムソケット経由で接続しようとしており、そのソケットが存在しないことを意味します。

config/database.yml ファイルの development セクションに port: 5432 を追加してみてください。

@Falco さん、こんにちは。
返信ありがとうございます。
同じ状況です。

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?
Couldn't create 'discourse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

Tasks: TOP => db:create
(See full trace by running task with --trace)

  url: postgresql://postgres::postgres@localhost:5432/discourse_development?pool=5

の代わりに、この行を追加してみてください。

@Falcoさん、ありがとうございます。
データベース.yml に不足している情報をすべて追加したところ、動作するようになりました。

#cat database.yml
development:
  prepared_statements: false
  adapter: postgresql
  database: <%= ENV['DISCOURSE_DEV_DB'] || 'discourse_development' %>
  min_messages: warning
  port: 5432
  host: localhost
  user: postgres
  password: mysecretpassword
  pool: 5
  timeout: 5000

これは、設定変数の読み込みに問題があるということでしょうか?

いや、それは単にデフォルトの socketport 宣言よりも優先されることを意味します。これを回避する一つの方法は url キーを使用することですが、他の方法については Rails のコードを確認することもできます。

「いいね!」 2