postgres ロールを新規作成してログインする

postgresユーザーでログインしようとするが、存在しない

❯ psql -h 127.0.0.1 -p 5432 -U postgres
psql: error: FATAL:  role "postgres" does not exist

ユーザー名を指定せずデフォルト設定のままでログイン

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

postgres=#

macのユーザー名でロールが作成されていた模様

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

postgresロールを作成

❯ createuser postgres

postgresロールが作成された

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

作成したpostgresロールに権限を追加する

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

postgresロールでログインできた

❯ psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
psql (13.6)
Type "help" for help.

postgres=#