Stackrig

Home Blog

tested run on a fresh Ubuntu 24.04, 6 October 2026 Run on Ubuntu 24.04 published

Peer authentication failed for user "postgres" on Ubuntu 24.04

We earn commissions when you shop through the links below.

We made “Peer authentication failed for user "postgres"” on a fresh Ubuntu 24.04, read why in the server's log, and logged in with one command.

Short answer: run sudo -u postgres psql instead of psql -U postgres. On 6 October 2026, on a fresh Ubuntu 24.04 with PostgreSQL 16.15, psql -U postgres as our own user failed with Peer authentication failed for user "postgres" and sudo -u postgres psql logged in. On this install the database user postgres is let in over the machine's socket only when the system user of the same name asks, and sudo -u postgres runs one command as that user.

What this was run on, and what it does not cover

We ran every command on this page on a fresh virtual machine at a cloud provider, started from Ubuntu's official 24.04 server image, where a script ran the commands and kept everything the machine printed. A block marked Output shows what the command above it printed, or the lines of it that matter, and a line is shown whole or without a start that the page names.

date -u +%Y-%m-%d
lsb_release -a
whoami
2026-10-06
Description:	Ubuntu 24.04.5 LTS
ubuntu

That is one release of Ubuntu, one machine, one day and one architecture, x86_64.

We installed PostgreSQL from Ubuntu's archive with the two commands below and changed nothing after that. This is the last line the first one printed.

sudo apt update
29 packages can be upgraded. Run 'apt list --upgradable' to see them.

Then the install. At a terminal apt asks whether to continue, and the script typed y.

sudo apt install postgresql
Do you want to continue? [Y/n] y
psql --version
psql (PostgreSQL) 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)

This page covers one cause of the error: that install as it stands, and a login as postgres from your own user. It doesn't cover the same error for a database user of your own, passwords, a change to PostgreSQL's configuration, or a connection from an application or another machine. Our guide to installing PostgreSQL on Ubuntu 24.04 has the first three of those.

In particular, this page doesn't edit pg_hba.conf, PostgreSQL's file for who may log in and how. Here is its checksum before the error, to compare at the end.

sudo sha256sum /etc/postgresql/16/main/pg_hba.conf
ad0df9635890926d79a12d5627b68af6b85b6254fa23cace2bbe077838969c9e  /etc/postgresql/16/main/pg_hba.conf

The error, made on purpose

This is the command that fails: a login as the database user postgres, named with -U, run as the system user ubuntu.

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

Why PostgreSQL refuses

The message doesn't say what failed to match. The server's own log does, in the three lines it wrote about the attempt, shown here without the date, the time and the process number at the start of each.

sudo tail -n 3 /var/log/postgresql/postgresql-16-main.log
postgres@postgres LOG:  provided user name (postgres) and authenticated user name (ubuntu) do not match
postgres@postgres FATAL:  Peer authentication failed for user "postgres"
postgres@postgres DETAIL:  Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 118: "local   all             postgres                                peer"

The provided name is the database user asked for with -U, the same name the error repeats after "for user", and the authenticated name is the system user the command ran as. The last line names the rule the server applied, line 118 of pg_hba.conf. For a connection over the machine's socket as postgres, that line says peer. PostgreSQL's manual describes the method in one sentence: it "works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name" (Peer Authentication). The kernel said ubuntu, the command asked for postgres, and the two are not the same name. These are the file's lines for socket connections, with their line numbers.

sudo grep -n "^local" /etc/postgresql/16/main/pg_hba.conf
118:local   all             postgres                                peer
123:local   all             all                                     peer
130:local   replication     all                                     peer

The fix: ask as the system user postgres

The machine has a system user called postgres, and sudo -u runs a command as another user (sudo(8)).

sudo -u postgres whoami
postgres

Run psql that way, without -U, and the name the kernel reports is the name of the database user. At psql's prompt the script typed \conninfo, which says how you are connected, and \q, which leaves.

sudo -u postgres psql
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=# \q

The two things we looked at are as before: the checksum of pg_hba.conf is the one from before, and psql -U postgres fails as it did.

sudo sha256sum /etc/postgresql/16/main/pg_hba.conf
ad0df9635890926d79a12d5627b68af6b85b6254fa23cace2bbe077838969c9e  /etc/postgresql/16/main/pg_hba.conf
psql -U postgres
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "postgres"

So the fix is the other command, not a repair of this one.

The app is free to try in your browser, without an account. To hear when Pro is ready, join the waitlist.

Sources

  • Our own run: a script on a fresh Ubuntu 24.04 machine on 6 October 2026. Every command and every output on this page is from the script and its log.
  • PostgreSQL 16 manual, 21.9 Peer Authentication: the sentence quoted above.
  • PostgreSQL 16 manual, 21.1 The pg_hba.conf File: what the file is, and that a local record is for connections over a Unix-domain socket.
  • Ubuntu 24.04's manual page sudo(8) on -u.
  • Stack Overflow question 18664074 (2013): where the error is asked about. We took nothing else from it.

Stackrig