0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker環境で発生したPostgreSQL接続エラーを解消した

Posted at

:dango: 書きたいこと:

Dockerを使用してWebアプリを作る過程で発生したActiveRecord::ConnectionNotEstablishedエラーをどうやって解消したか、記録に残しておく。

:hourglass_flowing_sand: 発生したエラー:

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 'app_development' database. Please check your configuration.
bin/rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory (ActiveRecord::ConnectionNotEstablished)
        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 (PG::ConnectionBad)
        Is the server running locally and accepting connections on that socket?

Tasks: TOP => db:create

:chart_with_upwards_trend: エラーの原因:

Docker 内で PostgreSQL に接続しようとするとき、TCP 接続 (host: db) を使う必要があるが、設定ミスでUNIX ドメインソケット (/var/run/postgresql/.s.PGSQL.5432) を使おうとしていた

:bulb: 解決法:

①設定ファイルに下記の内容を追加して、TCP接続を行うように設定

config\database.yml
development:
  <<: *default
  database: app_development
      host: dbname        # コメントアウトを解除し、localhostではなくデータベースサーバーのホスト名(コンテナ名)を設定
docker-compose.yml
services:
    environment:
      DATABASE_URL: postgres://username:password@severname:5432/dbname    # 設定値はサンプル

Docker compose upを実行

:santa: 結果:

http://localhost:3000/ でログインできた!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?