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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

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

Q&A

解決済

1回答

1862閲覧

posgresをDockerで動かしたい

ts21

総合スコア32

PostgreSQL

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

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

0グッド

1クリップ

投稿2020/04/12 02:14

編集2020/04/12 09:36

#環境
Mac os Catalina -v10.15.4
Ruby Mine
#やりたいこと
RailsのAPIをDockerでpostgresを動かしながら作成しようとしています
docker-compose.ymlは以下のようになります。

version: '3' services: db: image: postgres:12.2 environment: - DATABASE_HOST=localhost - POSTGRES_USER=$API_DB_DEV_USERNAME - POSTGRES_PASSWORD=$API_DB_DEV_PASSWORD - PGPASSWORD=$API_DB_DEV_PASSWORD ports: - "5432:5432" volumes: - datavol:/var/lib/postgresql/data volumes: datavol:

追記(04/12/18:37)
database.yamlは以下のようになります

# PostgreSQL. Versions 9.3 and up are supported. # # Install the pg driver: # gem install pg # On macOS with Homebrew: # gem install pg -- --with-pg-config=/usr/local/bin/pg_config # On macOS 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 # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> development: <<: *default database: bokin_back_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: bokin_back # 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: bokin_back_test # As with config/credentials.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 https://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: bokin_back_production username: bokin_back password: <%= ENV['BOKIN_BACK_DATABASE_PASSWORD'] %>

zshの環境変数にはPOSGRES_USERとPOSGRES_PASSWORDを記述しました.

開発ディレクトリにて、docker-compose up -dを実行して
rails db:migrateを実行したのですがエラーが出て、困っています

#エラー
rails db:migrateを実行したログがこちらです。

user@ts21-2 app_back % rails db:migrate Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`. /Users/ts/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /Users/ts/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here /Users/ts/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type.rb:27: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call /Users/ts/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.2/lib/active_record/type/adapter_specific_registry.rb:9: warning: The called method `add_modifier' is defined here rails aborted! PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"? /Users/ts/RubymineProjects/app_back/bin/rails:9:in `<top (required)>' /Users/ts/RubymineProjects/app_back/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

posgresのサーバに接続していないのが原因のようです
直接関係ないとbundlerのバージョンは2.1.4のはずですがwarningがでてました

ts@ts21-2 app_back % bundler -v Bundler version 2.1.4

#やったこと
同じエラーの記事を参考に
$ brew services start postgresqlと再起動を実行した上で、再びrails db:migrate
しましたが、上記エラーがでました

(そもそも、これはDockerと関係あるのか?)
#憶測
Dockerを立てて posgreを使うのが今回初めてなので、もしかするとDockerの設定に間違いがあるのか
と思っています。

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

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

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

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

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

hoshi-takanori

2020/04/12 09:31

config/database.yml の設定はどうなってますか? あと、docker とは別に brew でも postgresql を動かしてますか?
ts21

2020/04/12 09:37

config/database.ymを追記いたしました。 brew でも postgresql を動かしていました。
guest

回答1

0

ベストアンサー

このエラーが出るのは、pgsql などのクライアントから PostgreSQL にソケット接続を試みて失敗しています。

PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

今回の記述では、Docker Compose でポート 5432 を開いていますので、クライアント側からは TCP を通して、自分自身( localhost または 127.0.0.1 の )ポート 5432 に接続する必要があります。

確認手順としては、まず、 クライアント側から psql -h 127.0.0.1 -p 5432 -U ユーザ名 を接続して、接続できるかどうかの確認です。

もしここで接続できなければ、PostgreSQL (コンテナ)側の設定確認が必要です。 docker-compose ps で db サービスが起動しているかどうか確認します。もし停止していれば起動します。

次に database.yaml は初期状態に近いようですが、これをホスト 127.0.0.1 に、ポート 5432 で接続するように記述を変えると動作するでしょう。

投稿2021/04/10 22:00

zembutsu

総合スコア1584

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問