0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PostgreSQLのログイン時やユーザ変更時に"Peer authentication failed for user"と言われたら"pg_hba.conf"を変更する

Posted at

Goプログラミング実践入門 標準ライブラリでゼロからWebアプリを作る
でPostgreSQLをいじっているときに認証関係ではまってしまったのでメモ。

環境

* WSL2
* Ubuntu 20.04.5 LTS
* PostgreSQL 12.12 (Ubuntu 12.12-0ubuntu0.20.04.1)

エラー内容

psqlコマンドでユーザ指定してログインするときや、ログイン後にユーザを切り替えるときに"Peer authentication failed for user ..."(peer認証に失敗しています)と怒られる。
peer認証とは、systemのユーザネームと接続しようとしているデータベースのユーザネームがマッチしているかを検証するもの。
https://www.postgresql.org/docs/current/auth-peer.html

ターミナル
$ psql -U username # ログイン時
psql: error: FATAL:  Peer authentication failed for user "username"
PostgreSQL
\c tablename username # ユーザ切り替え時
psql: error: FATAL:  Peer authentication failed for user "username"

対処方法

postgresqlの認証関係は"pg_hba.conf"で設定されているので、これを修正することで解消できる。

ターミナル
sudo vi /etc/postgresql/12/main/pg_hba.conf # 場所はユーザごとに異なる
pg_hba.conf
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# 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                                     peer # <- ここをコメントアウト
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

pg_hba.confを更新後、postgresを再起動すればログインできるようになる。

ターミナル
$ sudo service postgresql restart # 再起動

$ psql -U username # ログイン
Password for user username:
psql (12.12 (Ubuntu 12.12-0ubuntu0.20.04.1))
Type "help" for help.

username=> # ログイン成功!
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?