PostgreSQL Peer authentication failed 해결
Peer authentication failed for user 는 소켓 접속에서 OS 사용자와 DB 역할 이름이 달라서입니다. 같은 이름으로 붙거나 -h 127.0.0.1 로 비밀번호 인증을 씁니다.
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "appuser"-h 없이 접속하면 psql 은 유닉스 소켓으로 붙고, 그 접속에 걸린 peer 방식은 지금 OS 사용자 이름을 접속하려는 DB 역할 이름과 비교합니다. 비밀번호와 상관없이 이름이 다르면 거절됩니다. 같은 이름의 OS 사용자로 접속하거나, psql -h 127.0.0.1 로 TCP 비밀번호 인증을 쓰거나, pg_hba.conf 의 그 줄을 바꿉니다. 소켓 peer · TCP scram-sha-256 은 Debian·Ubuntu 패키지에서 흔히 보는 구성입니다.
[1] 누가 어느 역할로 붙었는지 봅니다. 여기서는 OS 사용자 root 가 역할 appuser 와 postgres 로 붙으려 했습니다.
$ whoamiroot$ psql -U appuser -d appdbpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "appuser"$ psql -U postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"[2] 서버 로그에서 걸린 줄을 확인합니다. 클라이언트에는 보이지 않는 두 가지, 비교한 두 이름과 pg_hba.conf 의 몇째 줄에 걸렸는지가 남습니다.
LOG: provided user name (appuser) and authenticated user name (root) do not matchFATAL: Peer authentication failed for user "appuser"DETAIL: Connection matched file "/etc/postgresql/17/main/pg_hba.conf" line 3: "local all all peer"파일 위치는 패키지·이미지마다 다르니 서버에 물어봅니다.
$ sudo -u postgres psql -Atc "SHOW hba_file"/etc/postgresql/17/main/pg_hba.conf[3] 관리 작업이면 postgres OS 사용자로 접속합니다. 이름이 같으니 peer 를 통과합니다.
$ sudo -u postgres psql -c "SELECT current_user" current_user -------------- postgres(1 row)같은 원리로 OS 사용자 appuser 가 있으면 sudo -u appuser psql -d appdb 는 비밀번호 없이 appuser 역할로 붙습니다.
[4] 애플리케이션 계정이면 TCP 로 접속합니다. -h 127.0.0.1 을 주면 host 줄의 scram-sha-256 이 적용돼 비밀번호로 인증합니다.
$ PGPASSWORD='<비밀번호>' psql -h 127.0.0.1 -U appuser -d appdb -c 'SELECT current_user, inet_client_addr()' current_user | inet_client_addr --------------+------------------ appuser | 127.0.0.1(1 row)애플리케이션에서 이 오류가 나면 연결 문자열의 호스트가 비어 있거나 소켓 디렉터리로 잡혀 있지 않은지 봅니다. 비밀번호는 명령 줄 대신 ~/.pgpass 에 두면 셸 기록에 남지 않습니다. TCP 로 바꿨더니 password authentication failed for user 가 나오면 psql password authentication failed 해결로 넘어갑니다.
[5] 소켓 접속에도 비밀번호를 쓰려면 pg_hba.conf 를 바꿉니다. 먼저 지금 규칙을 봅니다. pg_hba_file_rules 뷰는 공식 문서대로 지금 디스크에 있는 파일의 내용을 보여 주며, 서버가 마지막으로 읽어 들인 규칙과 같다는 보장은 없습니다(실제로 쓰이는지는 서버 로그와 새 접속으로 확인합니다 — [6]). 위에서부터 처음 맞는 줄 하나만 적용되고, 그 줄에서 실패해도 아래 줄로 넘어가지 않습니다.
$ sudo -u postgres psql -c "SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules" line_number | type | database | user_name | address | auth_method -------------+-------+----------+------------+-----------+--------------- 2 | local | {all} | {postgres} | | peer 3 | local | {all} | {all} | | peer 4 | host | {all} | {all} | 127.0.0.1 | scram-sha-256 5 | host | {all} | {all} | ::1 | scram-sha-256(4 rows)파일을 복사해 둔 뒤(cp pg_hba.conf pg_hba.conf.bak) 3번 줄의 peer 만 scram-sha-256 으로 바꾸고 다시 읽게 합니다. 2번 postgres 줄은 그대로 두어야 관리자 접속이 막히지 않습니다. 재시작은 필요 없습니다.
$ grep -n '^local' /etc/postgresql/17/main/pg_hba.conf2:local all postgres peer3:local all all scram-sha-256$ sudo -u postgres psql -Atc "SELECT pg_reload_conf()"t$ PGPASSWORD='<비밀번호>' psql -U appuser -d appdb -c 'SELECT current_user' current_user -------------- appuser(1 row)$ psql -U appuser -d appdb -wpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: fe_sendauth: no password supplied이제 소켓 접속도 비밀번호를 요구합니다(-w 는 비밀번호를 묻지 말라는 옵션이라 바로 실패). 되돌릴 때는 복사본을 제자리에 놓고 pg_reload_conf() 를 다시 부릅니다.
[6] 다시 읽은 뒤에는 error 열을 확인합니다. 이 뷰는 지금 파일을 읽어 보여 주므로, 오류가 보이면 그 파일은 서버에 들어가지 않은 것입니다. pg_reload_conf() 가 t 를 돌려줘도 적용됐다는 뜻이 아닙니다. 방법 이름을 scram-sha256 처럼 잘못 적으면 서버 로그에 pg_hba.conf was not reloaded 가 남고 예전 규칙이 그대로 쓰입니다. 아래에서 마지막 접속이 비밀번호로 성공한 것은 고치기 전 규칙이 살아 있어서입니다.
$ sudo -u postgres psql -Atc "SELECT pg_reload_conf()"t$ sudo -u postgres psql -c "SELECT line_number, auth_method, error FROM pg_hba_file_rules WHERE error IS NOT NULL" line_number | auth_method | error -------------+-------------+---------------------------------------------- 3 | | invalid authentication method "scram-sha256"(1 row)$ PGPASSWORD='<비밀번호>' psql -U appuser -d appdb -Atc 'SELECT current_user'appuser[7] OS 사용자를 다른 역할에 묶고 싶으면 peer 를 두고 사용자 이름 매핑을 씁니다. pg_ident.conf 에 매핑을 적고 그 줄에 map= 을 붙입니다.
$ grep -v '^#' /etc/postgresql/17/main/pg_ident.conflab root appuserlab postgres postgres$ grep -n 'map=' /etc/postgresql/17/main/pg_hba.conf3:local all all peer map=lab$ sudo -u postgres psql -Atc "SELECT pg_reload_conf()"t$ psql -U appuser -d appdb -c 'SELECT current_user' current_user -------------- appuser(1 row)$ sudo -u appuser psql -d appdb -c 'SELECT current_user'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "appuser"root 는 appuser 로 붙었지만, 이름이 같아 전에는 붙던 OS 사용자 appuser 가 거절됐습니다. 매핑을 켜면 같은 이름도 매핑에 있어야 합니다(로그: no match in usermap "lab" for user "appuser" authenticated as "appuser"). 필요하면 lab appuser appuser 줄을 더합니다.
TCP 접속에서 Connection refused 가 나온다면 인증 전 단계입니다 — PostgreSQL Connection refused 해결을 봅니다.
공식 근거: Peer Authentication · pg_hba.conf · User Name Maps · pg_hba_file_rules
pg_hba.conf 규칙 대조기 도구로 바로 확인 ↗ — 브라우저 안에서 계산합니다.