Fix PostgreSQL "Peer authentication failed for user" in psql
Peer authentication failed for user: on a socket connection your OS user name differs from the role. Connect as the matching user or use -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"Without -h, psql connects over the Unix socket, and the peer method on that connection compares your current OS user name with the database role you asked for. If they differ, it refuses — the password doesn't matter. Connect as the OS user with the same name, use TCP password authentication with psql -h 127.0.0.1, or change that line in pg_hba.conf. Socket peer plus TCP scram-sha-256 is a layout you commonly meet with Debian and Ubuntu packages.
[1] Check who connected as which role. Here OS user root tried roles appuser and 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] Find the matching line in the server log. It shows two things the client never sees: the two names compared, and which line of pg_hba.conf matched.
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"The file location differs between packages and images, so ask the server:
$ sudo -u postgres psql -Atc "SHOW hba_file"/etc/postgresql/17/main/pg_hba.conf[3] For admin work, connect as the postgres OS user. The names match, so peer passes.
$ sudo -u postgres psql -c "SELECT current_user" current_user -------------- postgres(1 row)Likewise, if an OS user appuser exists, sudo -u appuser psql -d appdb gets in as role appuser with no password.
[4] For application accounts, connect over TCP. With -h 127.0.0.1 the host line with scram-sha-256 applies and you authenticate with a password.
$ PGPASSWORD='<password>' 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)If an application hits this error, check whether the host in its connection string is empty or points at the socket directory. Keep the password in ~/.pgpass instead of the command line so it stays out of shell history. If switching to TCP gives password authentication failed for user, go to Fix PostgreSQL password authentication failed for user.
[5] To require a password on socket connections too, edit pg_hba.conf. First look at the current rules. As the official docs say, the pg_hba_file_rules view shows the current contents of the file on disk, not necessarily what the server last loaded (whether rules are actually in use is checked with the server log and a new connection — see [6]). Only the first matching line applies; if it fails, later lines are not tried.
$ 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)Back up the file (cp pg_hba.conf pg_hba.conf.bak), change only peer on line 3 to scram-sha-256, and reload. Leave line 2 for postgres alone so admin access keeps working. No restart is needed.
$ 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='<password>' 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 suppliedSocket connections now need a password (-w means never prompt, so it fails at once). To roll back, put the backup in place and call pg_reload_conf() again.
[6] After every reload, check the error column. The view reads the file as it is now, so an error there means that file did not make it into the server. pg_reload_conf() returning t does not mean the file was applied. A typo such as scram-sha256 leaves pg_hba.conf was not reloaded in the server log and the old rules stay in force — the last login below succeeds only because the previous rules are still live.
$ 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='<password>' psql -U appuser -d appdb -Atc 'SELECT current_user'appuser[7] To tie an OS user to a different role, keep peer and add a user name map. Write the map in pg_ident.conf and add map= to the line.
$ 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 now gets in as appuser, but OS user appuser, which worked before because the names matched, is refused. With a map, even same-name logins must be listed (log: no match in usermap "lab" for user "appuser" authenticated as "appuser"). Add a lab appuser appuser line if you need it.
If TCP connections fail with Connection refused, the problem is before authentication — see Fix PostgreSQL Connection refused.
Official sources: Peer Authentication · pg_hba.conf · User Name Maps · pg_hba_file_rules
Check it with the pg_hba.conf rule checker tool ↗ — calculated in your browser.