質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

解決済

2回答

1726閲覧

DockerでPostgresの操作に失敗する

yanbou893

総合スコア7

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2020/01/31 03:05

DockerでRubyの環境構築をしています。

docker-compose run --rm back rails db:create

を実行すると以下のエラーが出ます。

could not connect to server: Connection refused Is the server running on host "db" (172.19.0.2) and accepting TCP/IP connections on port 5432? Couldn't create 'app_development' database. Please check your configuration. rails aborted! PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "db" (172.19.0.2) and accepting TCP/IP connections on port 5432? /usr/local/bundle/gems/pg-1.2.2/lib/pg.rb:58:in `initialize' /usr/local/bundle/gems/pg-1.2.2/lib/pg.rb:58:in `new' /usr/local/bundle/gems/pg-1.2.2/lib/pg.rb:58:in `connect' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:90:in `connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/postgresql_database_tasks.rb:12:in `connection' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/postgresql_database_tasks.rb:21:in `create' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:119:in `create' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:313:in `each' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:138:in `create_current' /usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <main>' /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-5.2.4.1/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require' /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency' /usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require' /app/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' /app/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)

datanase.ymlとdocker-compose.ymlは以下の通りです

datanase.yml # PostgreSQL. Versions 9.1 and up are supported. # # Install the pg driver: # gem install pg # On OS X with Homebrew: # gem install pg -- --with-pg-config=/usr/local/bin/pg_config # On OS X with MacPorts: # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config # On Windows: # gem install pg # Choose the win32 build. # Install PostgreSQL and put its /bin directory on your path. # # Configure Using Gemfile # gem 'pg' # default: &default adapter: postgresql encoding: unicode host: db # add username: postgres # add password: # add pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: app_development # The specified database role being used to connect to postgres. # To create additional roles in postgres see `$ createuser --help`. # When left blank, postgres will use the default role. This is # the same name as the operating system user that initialized the database. #username: app # The password associated with the postgres role (username). #password: # Connect on a TCP socket. Omitted by default since the client uses a # domain socket that doesn't need configuration. Windows does not have # domain sockets, so uncomment these lines. #host: localhost # The TCP port the server listens on. Defaults to 5432. # If your server runs on a different port number, change accordingly. #port: 5432 # Schema search path. The server defaults to $user,public #schema_search_path: myapp,sharedapp,public # Minimum log levels, in increasing order: # debug5, debug4, debug3, debug2, debug1, # log, notice, warning, error, fatal, and panic # Defaults to warning. #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: app_test # As with config/secrets.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix environment variable when you boot # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: app_production username: app password: <%= ENV['APP_DATABASE_PASSWORD'] %>
docker-compose.yml version: "3" services: db: container_name: sample_db image: postgres:11.4-alpine environment: - TZ=Asia/Tokyo volumes: - ./back/tmp/db:/var/lib/postgresql/data back: container_name: sample_back build: back/ volumes: - ./back:/app depends_on: - db ports: - 3000:3000 front: container_name: sample_front build: front/ command: npm run dev volumes: - ./front:/app ports: - 8080:3000

いろいろ試してみたのですがよくわかりませんでした。
すでに動いているのかと思い、db:createしても同様の結果でした。
ご教授いただければ幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

amura

2020/01/31 03:34

Postgresqlのpg_hba.confは設定されていますでしょうか?
yanbou893

2020/01/31 04:59

特に触っていません... 回答いただき、調べてみて場所の見当はおおよそでついたのですが、表示/編集の仕方がわからず詰まってしまいました...
amura

2020/01/31 05:48

pg_hba.confの設定はググって頂くとよいと思います。(trustで全員OKしているのも) 起動後psql -l -h IPアドレス にて接続確認ができます。lolcalhostはもともとOKのハズです
yanbou893

2020/01/31 06:20

ありがとうございます。 もう少し調べてみようと思います!!
bsdfan

2020/01/31 11:42

dockerhubから持ってきたpostgresのイメージならばpg_hba.confの設定は問題ないはずです。 それよりも、環境変数等の不足で初期化がうまくできずに、dbの起動に失敗している気がします。 docker-compose logs db で何が表示されますか?
yanbou893

2020/02/03 01:06

logsで見てみるとmysqlが動いていたので、停止・削除して再度実行しました。 Dockerの環境構築をテスト的にいじっているうちにエントリーポイントなるものにmysqlを登録していたみたいでした。 もう少し調べてみます。ありがとうございます
guest

回答2

0

自己解決

一度すべてのimage,コンテナを削除して、もう一度試したら解決しました

投稿2020/02/03 06:16

yanbou893

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

dbenvironmentsPOSTGRES_DB=app_developmentを書き足してみるといかがでしょうか?

投稿2020/01/31 14:21

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yanbou893

2020/02/03 00:36

ありがとうございます。同じ結果が返ってきました...
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問