SaaS architecture
Peer authentication failed for user "postgres": fixes
PostgreSQL's Peer authentication failed, measured on the Debian and Ubuntu packages: why sudo -u postgres works, -h localhost, pg_ident.conf and psycopg2.
FATAL: Peer authentication failed for user "postgres", which psql prints as psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres", means you connected over the Unix socket, the matching pg_hba.conf line said peer (or ident, which on a socket line becomes peer), and your operating-system user name was not the database role you asked for. On a PostgreSQL installed from the Debian or Ubuntu package (15.19 on Debian 12, 16.15 on Ubuntu 24.04), the default file has local all postgres peer, so psql -U postgres as root or any user other than postgres fails. The fastest fix is to become that OS user: sudo -u postgres psql.
The official postgres Docker image does not give this error out of the box: its local lines say trust, and psql -U postgres as root connected on 13, 15, 17 and 18. We measured both packages with the psql they ship, and the official images 13.23, 15.19, 17.11 and 18.6, on 28 October 2026, and repeated the core with the script at the end on 5 and 6 November 2026.
| Situation | What happens | Fix |
|---|---|---|
psql -U postgres as root or another OS user (package install), also with -h /var/run/postgresql | local … peer compares the OS user with the role: no match | sudo -u postgres psql |
psql -U nobody, a role that does not exist | the same peer error, not role does not exist | as above |
psql as root without -U | peer succeeds (root = root), then role "root" does not exist | sudo -u postgres psql, or create the role |
psql -U postgres -h localhost on a fresh install | a host … scram-sha-256 line: fe_sendauth: no password supplied, or password authentication failed | alter role postgres password '…' first |
an app runs as OS user app and connects as role appuser | peer fails, a password in the config or in ~/.pgpass is ignored | connect over TCP with a password, or a pg_ident.conf map |
| psycopg2 with no host, or an empty one | libpq takes the socket, so peer | host='localhost' plus a password |
you edited pg_hba.conf and nothing changed | the server still uses the old file | select pg_reload_conf(); and check the log |
| official Docker image | local … trust: no peer error | nothing, unless you set peer yourself |
What is peer authentication?
A check without a password: “The peer authentication method works by obtaining the client’s operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping).” And: “This method is only supported on local connections.” Local means the Unix socket, hence the socket path in the message; a message with a host and port came over TCP.
The default pg_hba.conf of the Debian 12 package:
90:local all postgres peer
95:local all all peer
97:host all all 127.0.0.1/32 scram-sha-256
99:host all all ::1/128 scram-sha-256
102:local replication all peer
Ubuntu 24.04 had the same lines, at 118 to 132: peer for every socket connection, not only for postgres.
A local line that says ident gives the same error. The documentation: “When ident is specified for a local (non-TCP/IP) connection, peer authentication (see Section 20.9) will be used instead.” With ident on the local all all line, pg_hba_file_rules showed peer, the client got Peer authentication failed, and only the DETAIL line in the log still quoted ident.
Why does sudo -u postgres psql work but psql -U postgres not?
Because peer compares names, and only the first runs as OS user postgres. As root, app and alice, psql -U postgres got the same client message, with exit code 2. The OS user name was only in the server log, /var/log/postgresql/postgresql-15-main.log, on a LOG line before the FATAL:
LOG: provided user name (postgres) and authenticated user name (root) do not match
FATAL: Peer authentication failed for user "postgres"
DETAIL: Connection matched pg_hba.conf line 90: "local all postgres peer"
- ClientAs OS user
root, runspsql -U postgreswithout a host, so libpq takes the Unix socket in/var/run/postgresql.Asks for rolepostgres - ServerFor role
postgresthe first matching line is 90,local all postgres peer, whatever the OS user.Method: peer - ServerGets the OS user of the socket's other end from the kernel and compares it with the role; there is no
map=on the line.root, not postgres - Server log
provided user name (postgres) and authenticated user name (root) do not match, then theFATALand theDETAILwith line 90.Both names, and the line - Client
FATAL: Peer authentication failed for user "postgres", exit code 2.Only the role; no OS user, no line
Measured as root, app and alice: the client message was identical each time; only the name after authenticated user name in the log changed. Any other role, nobody or root, matched line 95, local all all peer, instead.
sudo -u postgres psql connected as postgres, over the socket (inet_client_addr() is null returned t). For a system without sudo, su - postgres -c psql as root connected the same way. psql -U postgres -h /var/run/postgresql as root, on the other hand, is still the socket: a directory is not a host, and it gave the same peer error.
As root without -U, peer did not fail: the names matched (root = root), and the next check did, with FATAL: role "root" does not exist over the same socket. Our article on role does not exist starts from that message. A role that does not exist at all, psql -U nobody as alice, gave Peer authentication failed for user "nobody", not role does not exist: so this error does not tell you the role exists. In every case we ran, the name in the message was the role asked for, never the OS user. psql -U root as alice gave Peer authentication failed for user "root", with authenticated user name (alice) in the log: for user "root" comes from an OS user other than root asking for the role root.
From 16 on, the DETAIL line also names the file (Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 118); the client message was identical on 13, 15, 16, 17 and 18.
Why does -h localhost give a password error instead?
Because over TCP a host line applies, and that line asks for a password. On a fresh Debian install, before anything else:
$ psql -U postgres -h 127.0.0.1 -w -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied
That message comes from the client; the server logged nothing. With a password given, wrong or not, the role postgres still had none to compare it with:
FATAL: password authentication failed for user "postgres"
DETAIL: User "postgres" has no password assigned.
Connection matched pg_hba.conf line 99: "host all all ::1/128 scram-sha-256"
In our containers localhost became ::1 and met the IPv6 line. And psql printed the error twice, and the log had two FATAL lines. The Debian cluster has SSL on (show ssl is on); with sslmode=disable and with sslmode=require it was one line each, which fits libpq’s default sslmode=prefer: one attempt with SSL, then one without. More on both in password authentication failed for user and no pg_hba.conf entry for host.
How do you log in as postgres with a password?
Set one through the socket first, then use TCP:
-- sudo -u postgres psql
alter role postgres password '…';
-- then: PGPASSWORD=… psql -U postgres -h 127.0.0.1
-- current_user | inet_client_addr
-- postgres | 127.0.0.1
After that, a wrong password still failed, without the has no password assigned line. The socket stays peer: a password does not change how psql -U postgres without -h is checked.
How do you let another OS user log in as a database role?
With a user name map in pg_ident.conf. The documentation: “To use user name mapping, specify map=map-name in the options field in pg_hba.conf.” We let OS user app log in as role appuser, with a line above local all all peer, because the first matching line wins:
pg_hba.conf 95: local appdb appuser peer map=appmap
pg_ident.conf 43: appmap app appuser
After a reload, psql -U appuser -d appdb as app connected as appuser. As alice on the same line, the client message was unchanged, and the log line changed:
LOG: no match in usermap "appmap" for user "appuser" authenticated as "alice"
A typo in either file was silent. With map=appmpa (no such map) the error column of pg_hba_file_rules was empty and the reload gave no warning; with ap for app in pg_ident.conf, pg_ident_file_mappings showed no error either. Both surfaced only at the next connection, as no match in usermap in the log. If the map works and the role is missing, the error becomes role "appuser" does not exist.
Why does psycopg2 say Peer authentication failed when the password is set?
Because without a host, libpq uses the socket, and a peer line never asks for the password. As OS user alice, psycopg2 2.9.5 on Debian 12:
| Connection arguments | Result |
|---|---|
dbname='appdb', user='appuser' | Peer authentication failed for user "appuser" |
… host='' | the same, over the socket |
… password='…' (no host) | the same: the password was ignored |
… host='localhost' | fe_sendauth: no password supplied |
… host='localhost', password='…' | connected, from ::1 |
An empty PGHOST did the same for psycopg2 and for psql, and so did a ~/.pgpass entry for appuser: over the socket psql never used it. Django and Rails we did not test; with an empty host setting we expect the same, as they connect through libpq. node-postgres (pg 8.23.1) defaults to localhost, so it went over TCP and never hit peer; only with the socket directory as host (host: '/var/run/postgresql', or PGHOST set to it) did it fail, with code 28000 and without the FATAL: prefix or socket path.
I edited pg_hba.conf: why does it still fail, or still work?
Because the server reads the file at start-up and on a reload: “If you edit the file on an active system, you will need to signal the postmaster (using pg_ctl reload, calling the SQL function pg_reload_conf(), or using kill -HUP) to make it re-read the file.” On the official images we changed local all all trust to peer: root’s psql -U postgres kept working until select pg_reload_conf(), and failed with the peer error after it. Meanwhile pg_hba_file_rules already showed peer. The documentation says as much: “Note that this view reports on the current contents of the file, not on what was last loaded by the server.”
A broken line is worse. We changed peer to peeer and reloaded. pg_reload_conf() returned t, the view showed invalid authentication method "peeer", and the log said:
LOG: invalid authentication method "peeer"
CONTEXT: line 95 of configuration file "/etc/postgresql/15/main/pg_hba.conf"
LOG: pg_hba.conf was not reloaded
Connections kept working on the old rules. “Rows in the view with non-null error fields indicate problems in the corresponding lines of the file,” says the documentation: check it after every edit, because the t from pg_reload_conf() says nothing about the result.
Why don’t I get this error in Docker?
Because the official image trusts local connections. Its pg_hba.conf has local all all trust and host all all 127.0.0.1/32 trust, and inside the container (docker exec) both psql -U postgres and psql -U postgres -h localhost -w connected without a password on 13, 15, 17 and 18. From another container on the Docker network (measured on 17), the last line host all all all scram-sha-256 applied: fe_sendauth: no password supplied without one. With the local line set to peer, root got the same client message as on Debian, in docker logs the same LOG line, and -h 127.0.0.1 still connected without a password. So if you see this error, you are most likely on a distribution package, or on a file someone edited. The cases that come up in Docker instead are in role does not exist and Connection refused.
Switching a package install to trust would hide the error too. Inside the official image, trust let root in as postgres without any password; on a machine with several OS users, that is not a fix.
Reproduce it
run.sh starts a Debian 12 container with the postgresql package and the OS users app and alice, plus an official postgres:17 container, and prints the client messages and log lines above, except node-postgres, the connection from another container and the official images other than 17. It needs only Docker and removes its containers at the end; IMAGE=ubuntu:24.04 ./run.sh does the Ubuntu run.
What we did not measure
Rails (the pg gem), Django itself, asyncpg and psycopg 3. pg_dump, pg_restore and pg_dumpall: they connect through libpq like psql, so we expect the same message, but we did not run them. Peer authentication on macOS or the BSDs, and package installs from other distributions. Windows neither: the documentation names “Linux, most flavors of BSD including macOS, and Solaris” as the systems with peer authentication. Managed databases, and peer over ssh.
Sources: PostgreSQL documentation (version 18) on peer authentication, the pg_hba.conf file and user name maps, read on 28 October 2026; ident authentication and pg_hba_file_rules, read on 6 November 2026. All measurements are our own: the Debian 12 package (PostgreSQL 15.19, psycopg2 2.9.5, node-postgres 8.23.1), the Ubuntu 24.04 package (16.15) and the official images 13.23, 15.19, 17.11 and 18.6, on 28 October 2026; the script on 5 and 6 November 2026.