LoginSignup
1
0

More than 5 years have passed since last update.

【Ruby on Rails】production環境でデータベースを作成する際にPostgreSQLにログインできない場合の対処法

Last updated at Posted at 2019-02-20

Ruby on Railsでproduciton環境でデータベースを作成する際にいくつかハマりポイントがあったのでメモ

まずはmaster.keyで

作業するディレクトリを変えたかった関係でcloneしていたのでまずここでつまずきました。
Rails5.2から追加された機能のようですね。
こちらの記事を参考にさせていただいた結果この現象についてはすぐに解決。
Rails 5.2 で ActiveSupport::MessageEncryptor::InvalidMessage

Peer認証

PostgreSQLにログインできるかどうか試してみる。

$ psql -U taskcontrol
psql: FATAL:  Peer authentication failed for user "taskcontrol"

どうやらPeer認証というPostgreSQLの機能でOSのユーザー名とPostgreSQLのユーザー名が違うとログインをはじいてしまう仕組みがあり、この機能がデフォルトでオンになっていた模様。
PostgreSQLのPeer認証と他の認証方法への変更
こちらの記事を参考に/etc/postgresql/10/main/pg_hba.confを編集し、最下部のpeerとなっている部分をすべてmd5に変更。

pg_hba.conf
# Database administrative login by Unix domain socket
local   all             postgres                                md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

もう一度ログインしようとすると同じエラー。PostresSQLを再起動する必要があった。

$ sudo service postgresql stop
$ sudo service postgresql start

再度ログイン

$ psql -U taskcontrol
Password for user taskcontrol:
psql: FATAL:  database "taskcontrol" does not exist

またログインできていないのかと思ったが、データベースをまだ作成していないだけだったので、production環境でのデータべース作成を実行。

$ RAILS_ENV=production bin/rails db:create db:migrate
== 20190212064526 CreateTasks: migrating ======================================
-- create_table(:tasks)
   -> 0.0223s
== 20190212064526 CreateTasks: migrated (0.0234s) =============================
(中略)
== 20190215013543 AddUserIdToTasks: migrating =================================
-- execute("DELETE FROM tasks;")
   -> 0.0011s
-- add_reference(:tasks, :user, {:null=>false, :index=>true})
   -> 0.0120s
== 20190215013543 AddUserIdToTasks: migrated (0.0153s) ========================

まとめ

なんとかproduction環境でデータベースを作成できました。
何か捕捉の情報や修正などありましたら、コメント欄にてご指摘頂けますと幸いです。

1
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
1
0