PostgreSQL導入備忘録(peer認証エラー)
環境
・Ubuntu18.04(WSL2)
・postgres (PostgreSQL) 11.2
問題
$ psql -U postgres -d postgres
コマンドでpsqlにログインできず、
psql: error: FATAL: Peer authentication failed for user "postgres"
なるエラーが表示される
原因
・peer認証が有効になっている
peer認証とは、ローカルからのアクセス時にpostgres(OS側)のユーザー名がpostgres(データベース側)のものと一致している場合のみ接続を許可する認証方法で、ここが不一致のために怒られているらしい
解決策
・postgres(OS側)のパスワードを設定
・postgres(データベース側)のパスワードを設定
・クライアント認証設定ファイルのgpg_hba.confを書き換える
$ sudo passwd postgres
以下のSQL文を実行してパスワードを設定
ALTER USER postgres with encrypted password 'パスワード';
次にfindコマンドでpg_hba.confを探します
$ sudo find / -name pg_hba.conf
自分の環境では
/etc/postgresql/13/main下にありました
# Database administrative login by Unix domain socket
local all postgres peer
の行をみてみる
postgresユーザーからの接続がデフォルトではpeerとなっているので、ここを
peer→md5
と変更
$ /etc/init.d/postgresql restart
でpsqlを再起動
補足
公式ドキュメント
に従ってインストール後、psqlコマンドを実行すると
error: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
というエラー文が表示される
$ pg_lsclusters
でpsqlのstatusを確認→サーバーがダウンしている模様
10 main 5432 down postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
$ /etc/init.d/postgresql start
で起動
statusがonline postgresになっていればOK