Skip to content

FATAL: Peer authentication failed for user 'postgres', Fixed

'Peer authentication failed for user' means PostgreSQL asked the kernel who you are and your Linux username didn't match the role you asked for. Why sudo -u postgres psql works and psql -U postgres doesn't, and the fixes that don't involve trust.

peer-authentication ident-authentication pg-ident-conf sqlstate-28000 pg-hba pg-auth postgresql postgresql-errors
Gopi Gorantala
Reading Progress

On This Page

Here's the bit that confuses everyone. On the same Ubuntu box, in the same minute, sudo -u postgres psql drops you straight into a prompt, no password asked. And psql -U postgres fails with Peer authentication failed for user "postgres". Same server, same role, same socket. One works and one doesn't, and the only thing that changed is which Linux user typed the command.

That's the whole error, really. PostgreSQL didn't check a password at all. It asked the operating system who was on the other end of the socket, got back root (or deploy, or alice), and compared that with the role you asked for. They didn't match, so it said no.

What the server log said

This is what the client prints. I ran it as root against PostgreSQL 16.15 from Ubuntu 24.04's packages, with psql 16.15:

$ psql -U postgres
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "postgres"

And this is the server log for the same attempt, which tells you a lot more:

2026-12-02 11:24:08.194 UTC [273] postgres@postgres LOG:  provided user name (postgres) and authenticated user name (root) do not match
2026-12-02 11:24:08.194 UTC [273] postgres@postgres FATAL:  Peer authentication failed for user "postgres"
2026-12-02 11:24:08.194 UTC [273] postgres@postgres DETAIL:  Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 118: "local   all             postgres                                peer"

The LOG line is the answer. "Provided user name" is the role you typed. "Authenticated user name" is what the kernel says you are. The DETAIL names the exact pg_hba.conf line that picked the peer method. On 15 and older that line reads Connection matched pg_hba.conf line 118 without the file path, and pre-14 clients print the shorter psql: FATAL: Peer authentication failed for user "postgres" without the socket path in front.

The SQLSTATE is 28000 (invalid authorization specification), not the 28P01 you get from password authentication failed. With log_error_verbosity = verbose the log shows it came from check_usermap in hba.c, so it's the postmaster's authentication code that raised it, before your session ever existed. If your server's lc_messages isn't English the text is translated. The SQLSTATE isn't.

Socket or TCP? That decides everything

Peer authentication only exists on Unix-domain socket connections, the local lines in pg_hba.conf. So the first question is how you connected, and the client already told you: connection to server on socket "…/.s.PGSQL.5432" means socket. If it had said connection to server at "localhost" (127.0.0.1), port 5432, you'd be on TCP, and peer would never have been involved.

You get a socket whenever you don't give libpq a host: no -h, no host= in the connection string, no PGHOST. That's psql with no flags, Django with HOST: '', Rails with no host: in database.yml. Java never hits this. pgJDBC talks TCP unless you've gone out of your way to wire up a socket factory, so if your Spring app gets an authentication error, it's a different one.

Two commands settle it on the database host:

grep -nE '^\s*local' /etc/postgresql/*/main/pg_hba.conf      # which socket lines exist, and their methods
sudo -u postgres psql -c "select line_number, database, user_name, auth_method, options from pg_hba_file_rules where type = 'local'"

If the line from your DETAIL says peer, or ident (more on that in a minute), you're in the right article.

Reproduce it on a stock Ubuntu cluster

No Docker this time, on purpose. The official postgres image sets up trust for socket connections inside the container, so you'll almost never see peer failures there. This error lives on distro packages, which is where most people meet it: a fresh VM, apt install postgresql, and the first psql fails.

What I used: Ubuntu 24.04, the postgresql-16 package (16.15), the Debian postgresql-common wrappers (pg_ctlcluster), and a disposable VM. The shipped pg_hba.conf, comments stripped:

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

Step 1, start the cluster and make a role for a ledger-posting job. Run these as separate commands:

pg_ctlcluster 16 main start
sudo -u postgres psql -c "CREATE ROLE ledger_app LOGIN PASSWORD 'lab-only-pw'"
sudo -u postgres psql -c "CREATE DATABASE ledger OWNER ledger_app"

Separate on purpose. My first attempt put all three statements in one psql -c, and psql sends that as a single implicit transaction. CREATE DATABASE cannot run inside a transaction block failed, the whole thing rolled back, and the role I'd "created" didn't exist. Which turned into the most interesting result of the afternoon, see below.

Step 2, fail it. As root:

$ psql -U ledger_app -d ledger -c 'select 1'
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "ledger_app"

Step 3, prove it's the OS username and nothing else. Same role, same database, but over TCP with the password, then over the socket as a Linux user with the matching name:

$ PGPASSWORD=lab-only-pw psql -h localhost -U ledger_app -d ledger -c 'select current_user, inet_server_addr()'
 current_user | inet_server_addr
--------------+------------------
 ledger_app   | 127.0.0.1
(1 row)

$ useradd -m ledger_app
$ su ledger_app -c "psql -d ledger -c 'select current_user, session_user'"
 current_user | session_user
--------------+--------------
 ledger_app   | ledger_app
(1 row)

Both work. You reproduced it if the socket attempt fails with exactly that message and the server log shows authenticated user name (root). If you get FATAL: role "root" does not exist instead, you ran plain psql with no -U: libpq defaults the role to your OS username, peer passed (root is root), and then the role lookup failed. Different article: role does not exist.

Teardown: sudo -u postgres psql -c 'DROP DATABASE ledger' -c 'DROP ROLE ledger_app', userdel -r ledger_app, put back any pg_hba.conf or pg_ident.conf you edited, pg_ctlcluster 16 main stop.

What's going on, roughly

The local line that matched your connection says peer. Peer means "the database role must equal the operating system username of the process on the other end of the socket". You're logged in as one Linux user and asked for a different role. No password was ever going to be requested, so no password could fix it.

The kernel vouches for you, and the server believes it

A Unix-domain socket is a file-like endpoint on the same machine, so both ends are processes on one kernel. On Linux the server can ask that kernel for the connecting process's user ID (SO_PEERCRED; other systems use getpeereid()), and the kernel's answer can't be forged by the client. PostgreSQL turns that UID into a username and compares it with the role in the startup packet. The peer authentication docs for 19 list Linux, most BSDs including macOS, and Solaris. Windows isn't on the list.

The comparison runs through check_usermap. Without a map= option it's a plain string match. With one, it looks the pair up in pg_ident.conf, and failure there gets its own log line, which is how I found out I'd typed map=opps:

LOG:  no match in usermap "opps" for user "ledger_app" authenticated as "root"
FATAL:  Peer authentication failed for user "ledger_app"

Now the surprise. Remember the role that silently rolled back in step 1? Before I noticed, I'd run psql -U ledger_app against a database where ledger_app didn't exist. I got Peer authentication failed, not role does not exist. I tried it again on purpose with -U nosuchrole, from root and from an unrelated OS user, and got peer failure both times. Peer compares names before anyone checks whether the role is real. So "fix the peer error" can leave you staring at the next error. Check the role with \du once you're in as postgres.

The other thing I'd half-forgotten: ident on a local line isn't ident. The authentication methods page says socket connections using ident are treated as peer, and the lab agrees. I set line 123 to ident and got Peer authentication failed, with the DETAIL still quoting the ident line. Confusing, but consistent.

Ident authentication failed (RHEL, Rocky, CentOS and friends)

Over TCP, ident is a different beast. The server opens a connection back to an ident daemon on the client host, port 113, and asks which user owns your socket. Almost nobody runs identd any more, so it fails like this:

$ PGPASSWORD=lab-only-pw psql -h 127.0.0.1 -U ledger_app -d ledger -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL:  Ident authentication failed for user "ledger_app"
LOG:  could not connect to Ident server at address "127.0.0.1", port 113: Connection refused
FATAL:  Ident authentication failed for user "ledger_app"
DETAIL:  Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 126: "host    all             all             127.0.0.1/32            ident"

Note the password went in and did nothing. If you see Ident authentication failed on a host line, read your distro's pg_hba.conf rather than assuming; some setups put ident on the loopback lines. The fix is the same as below: scram-sha-256 for those lines, then reload.

Fixing peer authentication failed without reaching for trust

The top answers on this error say change peer to trust. Please don't. On a local all all line, trust lets any OS user on the box connect as any role, postgres included, with no password at all. Every cron job, every compromised web process, every intern's shell. It also hides the mismatch instead of fixing it.

Pick by who's connecting:

Who's connecting Fix Cost
You, doing admin work sudo -u postgres psql none; this is what the default is for
An app or script as a named role connect over TCP with a password: psql -h localhost -U ledger_app -d ledger needs a password and a .pgpass or secret
A service account that should be a different role peer map=… plus a pg_ident.conf entry two files to keep in sync
App roles that should always use passwords, even on the socket change only the generic local all all line to scram-sha-256 every socket login now needs a password

For the app, the change is usually just the host. In Django, HOST: 'localhost' instead of ''. In a libpq URL, postgresql://ledger_app@localhost/ledger instead of postgresql:///ledger. That moves you onto the host … scram-sha-256 lines. (Connection string formats covers every variant.) If you then get password authentication failed, it's the role's password or the role itself: that's the 28P01 article.

For a map, this is what worked in the lab. The new line goes above the generic one, because the first matching line wins:

# pg_hba.conf
local   all             postgres                                peer
local   ledger          ledger_app                              peer map=ops
local   all             all                                     peer
# pg_ident.conf
# MAPNAME   SYSTEM-USERNAME   PG-USERNAME
ops         root              ledger_app
ops         ledger_app        ledger_app

The second map entry matters. Once a line has map=, the plain same-name match no longer applies on its own, so the real ledger_app OS user needs its own row.

Then reload, which is enough for both files (no restart):

sudo -u postgres psql -c 'select pg_reload_conf()'    # or: pg_ctlcluster 16 main reload / systemctl reload postgresql
sudo -u postgres psql -c 'select map_name, sys_name, pg_username, error from pg_ident_file_mappings'
sudo -u postgres psql -c "select line_number, auth_method, options, error from pg_hba_file_rules where type = 'local'"

pg_ident_file_mappings exists from 15. Both views read the files on disk, not what the server loaded, so they show mistakes before you reload. Mine showed {map=ops} on line 123, and psql -U ledger_app -d ledger as root came back with current_user = ledger_app.

Editing pg_hba.conf needs root on the database host. On a managed service you can't, and you won't see peer anyway: you're always on TCP there.

And if what you actually wanted was to log in as postgres from your own account? sudo -u postgres psql, then \password postgres if you want a password for TCP logins too. That's the intended path, not a workaround.

Designing around it

Keep peer for postgres on the socket. It's the right tool: only someone who can become the postgres OS user gets superuser, and there's no password to leak.

Give applications their own roles and make them come in over TCP with SCRAM passwords, from a secret store, with the host named explicitly in config. Never let an app fall back to the socket because a DB_HOST variable was empty. That's the version of this that bites in production: a deploy where the host variable didn't render, the driver quietly chose the socket, and the error says "peer" when the real bug is a missing environment variable. The config I'd ship for the ledger job:

DB_URL=postgresql://ledger_app@db-host.internal:5432/ledger?sslmode=verify-full
# password from PGPASSFILE or the secret store, never the URL

Use pg_ident.conf maps only for operator accounts you control, and keep the file in config management next to pg_hba.conf.

Which versions behave how

Peer has worked the same way for years. What changes is the wording around it. Libpq 14 and later prefixes the socket path (connection to server on socket "…" failed:); older clients don't. Server 16 and later prints Connection matched file "<path>" line N; 15 and earlier say Connection matched pg_hba.conf line N. pg_ident_file_mappings arrived in 15, pg_hba_file_rules in 10. I checked messages on 16.15 and the method docs on 19, which is current (15 to 19 are supported as of December 2026).

To catch it early: in CI, provision a VM from the same package as production and run your app's migration step as the service user, not as root. And alert on failed logins in the server log. log_connections = on plus a count of FATAL lines with SQLSTATE 28000 or 28P01 per hour will show a misrendered host variable within a minute of the deploy, not when a customer files a ticket.

On upgrades, Debian's pg_upgradecluster carries your pg_hba.conf across, but a hand-built new cluster gets the stock file back, peer lines and all. Diff the two files before you cut over.

What to remember

  • Peer means "your Linux username must equal the role", and only on the Unix socket. A password can't fix it.
  • The server log's provided user name (X) and authenticated user name (Y) line tells you both names. Read it first.
  • -h localhost moves you to the TCP lines and password auth. That's often the whole fix for an app.
  • Never swap peer for trust. Use sudo -u postgres, a SCRAM line for app roles, or a pg_ident.conf map.
  • Peer fails before the role lookup, so a missing role can hide behind it.

Short answers to the follow-up searches

Why does sudo -u postgres psql work but psql -U postgres doesn't?

With sudo -u postgres the process runs as the postgres OS user, so the kernel reports postgres and the peer check passes. With psql -U postgres from your own account, the kernel reports your username, which doesn't match. Same server, same pg_hba.conf line, different OS user.

How do I fix "Ident authentication failed for user" on RHEL or Rocky Linux?

On a host line, ident asks an ident daemon on port 113 who owns your connection, and nothing usually answers. Change that line's method to scram-sha-256, make sure the role has a SCRAM password, and reload with select pg_reload_conf(). On local lines, ident behaves as peer and the error says "Peer" instead.

Do I need to restart PostgreSQL after editing pg_hba.conf or pg_ident.conf?

No. Both are re-read on reload: select pg_reload_conf(), pg_ctlcluster <version> main reload, or systemctl reload postgresql. Check pg_hba_file_rules and pg_ident_file_mappings for an error column first, because a line with an error is skipped. More in pg_hba.conf explained.

Why does Django or Rails get peer authentication failed when psql with a password works?

An empty or missing host makes the driver use the Unix socket, which lands on the local … peer line. Set the host to localhost (or the real database host) so the app uses TCP and its password. If you then get password authentication failed, the password or role is the problem, not the method.

peer-authenticationident-authenticationpg-ident-confsqlstate-28000pg-hbapg-authpostgresqlpostgresql-errors

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments