Postgres初心者ですpsql -U postgres
でログインしようとしたところパスワードが合致せず詰まったので、備忘録としてメモ、下記記事を参考にさせていただきました。
解決方法
pg_hba.conf
ファイルを探します。
$ find / -print |grep pg_hba.conf
-> /var/lib/pgsql/data/pg_hba.conf
vimを使ってファイルを編集しましょう
$ vim /var/lib/pgsql/data/pg_hba.conf
下にスクロールして以下のように表示されている場所を探します。
# 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 password
# IPv6 local connections:
host all all ::1/128 password
これを下記のようにMETHODの部分をTrust
変更します。
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
postgresを再起動します。
$ service postgresql restart
するとログインできるようになっています
psql -U postgres
postgres=#
パスワードを変更する
このままではパスワードが設定されていなくセキュアではないので、postgreユーザのパスワードを変更してpg_hba.conf
ファイルを元に戻しましょう
postgres=# ALTER ROLE postgres WITH PASSWORD {'パスワード'};
ALTER ROLE
そしたらpg_hba.conf
ファイルの設定を戻します。posgresからログアウトして再びvimを使います。
$ vim /var/lib/pgsql/data/pg_hba.conf
# 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 password
# IPv6 local connections:
host all all ::1/128 password
元に戻して一件落着!
これでログイン時にパスワードが聞かれるようになります。