Dev & EngARTICLE

7 min read

Peer authentication failed for user postgres: why this error appears and how to actually fix it

Peer authentication failed for user postgres: why this error appears and how to actually fix it
Image: Roberto Diniz
Article · Dev & Eng
Reading
7 min
Tables
1
Code
5
In short

A survey published on Planet PostgreSQL mapped, case by case, why psql -U postgres fails on Debian and Ubuntu installs and why the same account logs in without a password on the official Docker image.

Anyone who runs PostgreSQL in production in Brazil has run into this: you run psql -U postgres and get FATAL: Peer authentication failed for user "postgres". A survey published on Planet PostgreSQL, under the Now-Next blog, went after the exact cause, testing the Debian 12 (PostgreSQL 15.19) and Ubuntu 24.04 (16.15) packages against the official Docker images (13.23, 15.19, 17.11, and 18.6). The measurements were taken on October 28, 2026, with the core of the test repeated on November 5 and 6.

The result matters to any team that spins up Postgres via apt install and finds the behavior strange compared to anyone who only knows the official container: these are two different authentication worlds by default, and the difference isn't a bug, it's configuration.

What peer authentication actually is

The error isn't about a wrong password. It's about the operating system identity not matching the role requested in the database. PostgreSQL's documentation is direct about the mechanism:

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).

PostgreSQL's official documentation, version 18

This method only applies to local connections, via Unix socket, never over TCP. That's why the error message cites a path like /var/run/postgresql/.s.PGSQL.5432: it gives away that the connection went through the socket, not the network.

Why the Debian package is like this from the start

The default pg_hba.conf in the Debian 12 package has these lines, among others:

local all postgres peer
local all all peer
host all all 127.0.0.1/32 scram-sha-256

On Ubuntu 24.04 the content is equivalent, just on different lines of the file. Any connection via socket, for any role, falls under peer. Running psql -U postgres as root, or as any system user that isn't literally postgres, hits the first matching rule (line 90, in the tested case) and fails, because the OS user name (root) doesn't match the requested role (postgres).

The server log shows both names, something the client never sees:

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"

This explains why sudo -u postgres psql fixes it instantly: the process starts running as the system user postgres, and peer compares postgres with postgres. It's not magic, it's string equality.

The most common situations, summarized

SituationWhat happensFix
psql -U postgres as root or another OS userpeer compares names and they don't matchsudo -u postgres psql
psql as root, without -Unames match (root = root), but the root role doesn't existcreate the role, or use sudo -u postgres psql
psql -U postgres -h localhost on a fresh installfalls under a host ... scram-sha-256 line, which requires a passwordset a password with ALTER ROLE first
app runs as system user app and connects as role appuserpeer fails, password in .pgpass is ignoredmapping in pg_ident.conf, or TCP with password

When the error changes shape: TCP requires a password

Using -h localhost or -h 127.0.0.1 switches to a different line in pg_hba.conf: the connection becomes TCP, and the rule that applies is usually scram-sha-256. On a freshly created Debian install, before any password has been set, psql itself refuses the attempt:

$ 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

The correct path, according to the survey, is to set the password over the socket (where peer still works as root turned into postgres) and only then connect over TCP:

sql
-- via sudo -u postgres psql
ALTER ROLE postgres PASSWORD '...';

After that, PGPASSWORD=... psql -U postgres -h 127.0.0.1 connects normally. The socket stays on peer: changing the password doesn't change how psql -U postgres without -h is verified.

Mapping the OS user to a role with pg_ident.conf

The more correct solution for apps that run under a system user different from the database role is name mapping, not disabling peer. The documentation describes the mechanism: just declare map=map-name on the pg_hba.conf line and define the correspondence in pg_ident.conf. The Planet PostgreSQL test illustrates this with a typical case: the process runs as system user app, but needs to connect as role appuser.

# pg_hba.conf
local appdb appuser peer map=appmap

# pg_ident.conf
appmap app appuser

After a pg_reload_conf(), a connection as user app started authenticating as appuser with no password at all. One point of caution raised in the material: a typo in either of the two files is silent. A map=appmpa (nonexistent name) doesn't trigger a warning in pg_hba_file_rules, nor does pg_reload_conf() complain; the problem only shows up on the next connection attempt, as no match in usermap in the log. It's worth checking both views after any edit, not trusting the t return of the reload function.

The psycopg2 case (and why the password seems to be ignored)

A detail that catches Python applications by surprise: without host set, or with host='', libpq uses the socket by default, not localhost. This also applies to node-postgres, but there the default is already localhost, so the problem only shows up if someone explicitly points to the socket directory. With psycopg2 2.9.5, the test showed that password='...' without host simply doesn't factor in: the peer line never asks for a password, so it's ignored, and the error persists. Only with host='localhost' does the connection start requiring (and accepting) the password.

This debunks a common assumption: thinking it's enough to set the password in DATABASE_URL or .pgpass to fix it. If the connection string doesn't define a host, or defines an empty one, the application is under the same peer regime as psql without -h.

Editing pg_hba.conf and nothing changing

PostgreSQL only re-reads the file at startup or when it receives a reload signal. The pg_hba_file_rules view shows the current content of the file on disk, not what the server is actually using: "this view reports the current contents of the file, not what was last loaded by the server," the documentation says. In the test, swapping trust for peer in an official Docker image only took effect after select pg_reload_conf();: until then, the old connections kept going through.

Worse still: a corrupted line (peeer instead of peer) makes the reload fail silently at the client level. pg_reload_conf() returns t, but the log records invalid authentication method "peeer" and pg_hba.conf was not reloaded: the old rules remain in effect. The practical recommendation is to check pg_hba_file_rules after every edit, because the function's positive return doesn't guarantee the change took effect.

Why the official Docker image doesn't reproduce the problem

The official postgres image on Docker Hub uses local all all trust and host all all 127.0.0.1/32 trust by default. Inside the container, both psql -U postgres and psql -U postgres -h localhost -w connect without asking for anything, across all tested versions (13, 15, 17, and 18). It's an isolated development environment, with a single relevant system user, so trust isn't an immediate risk there. In production, on a Debian or Ubuntu machine with multiple system users, repeating that configuration would mean giving up any local access control: any process running as any user would get in as postgres with no password.

The survey's warning is direct: if you see this error, it's a sign you're on a distribution package, or in a file someone edited manually, not on Docker's default image.

What the test didn't cover

The survey was explicit about its limits: it didn't test Rails (the pg gem), Django, asyncpg, psycopg 3, or pg_dump, pg_restore, and pg_dumpall (equivalent behavior is expected, since they use libpq, but it wasn't measured). Also left out were macOS, BSDs, other Linux distributions, managed databases, and peer over SSH. For anyone administering Postgres outside of plain Debian/Ubuntu, it's worth treating the conclusions as a starting point, not a universal guarantee.

Translated from the Brazilian Portuguese original · Read the original