LoginSignup
0
0

More than 1 year has passed since last update.

久々にアプリをローカルサーバーで立ち上げようとしたらPG::ConnectionBad (FATAL: role "postgres" does not exist)

Posted at

久々にアプリをローカルサーバーで立ち上げようとしたら、PG::ConnectionBad (FATAL: role "postgres" does not exist):になりました。

postgresなんてroleはないぞと。
database.yml(下記参照)に定義しているDBのusernameを見て、そんなのがpostgresにないぞと言っているのかな?

config/database.yml
default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  encoding: unicode
  username: postgres
  password:
  host: localhost

postgresのユーザー(ロール?)を見てみる。

$ psql postgres
psql (13.6)
Type "help" for help.

postgres=# \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 kakudaisuke | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres-# \q

確かにいない。てか、いなくなってる。

一回postgresを\qで抜けて、ユーザーを作ってあげる。

$ createuser postgres

で、もう一回見てみる。

❯ psql -U postgres
psql (13.6)
Type "help" for help.

postgres-> \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 kakudaisuke | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 postgres    |                                                            | {}

ユーザーできた!
これで再挑戦。

❯ rails db:create
rails aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR:  permission denied to create database


Caused by:
PG::InsufficientPrivilege: ERROR:  permission denied to create database

先ほどのユーザーリストを見るとわかるけど、ロールpostgresに何も権限がない。
ALTER ROLEコマンドでDBを作る権限を与えてあげる。

❯ psql -U postgres
psql (13.6)
Type "help" for help.

postgres=> ALTER ROLE postgres WITH CREATEDB
postgres-> \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 kakudaisuke | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 postgres    |                                                            | {}

↑あれ、何も変わってない。
別の方法でログインしてやってみる。

❯ psql -U postgres -h localhost
psql (13.6)
Type "help" for help.

postgres=> ALTER ROLE postgres WITH CREATEDB;
ERROR:  permission denied
postgres=> \q

シカトされる状態から、素っ気ないながらもエラーの内容を教えてくれるまでは心の距離が縮まった。
ありがとう。権限があるユーザーで実行しなければいけないのか。
psql -U kakudaisukeではpsql: error: FATAL: database "kakudaisuke" does not existと言われてしまう。何かDBを指定してあげないといけないのか。DB一覧を再確認。

一部省略
$ psql -l -U postgres -h localhost
                                            List of databases
              Name              |    Owner    | Encoding | Collate | Ctype |      Access privileges
--------------------------------+-------------+----------+---------+-------+-----------------------------
 myapp_development            | kakudaisuke | UTF8     | C       | C     |
 postgres                       | kakudaisuke | UTF8     | C       | C     |

とりまpostgresってDBを指定してみる(何のDBか身に覚えがなく、わからないけど...)。

$ psql -U kakudaisuke -d postgres
psql (13.6)
Type "help" for help.

postgres=# ALTER ROLE postgres WITH CREATEDB;
ALTER ROLE
postgres=# \du
                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 kakudaisuke | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 postgres    | Create DB                                                  | {}

権限もきちんと設定された。
これでうまく行きました。

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