Fix: psql: FATAL: Peer authentication failed
On local socket connections PostgreSQL checks that your OS user name equals the database role. When they differ you get this error. Connect as the matching OS user, use TCP with a password, or change pg_hba.conf.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"
psql: FATAL: Peer authentication failed for user "appuser"
FATAL: Peer authentication failed for user "myapp"
For connections over the local Unix socket, Debian and Ubuntu’s default pg_hba.conf uses the peer method: PostgreSQL asks the kernel which operating system user is on the other end of the socket and only accepts the connection if that name equals the database role you asked for. You ran psql -U postgres as alice, the names differ, and no password can fix that because peer does not use passwords.
Cause 1: running psql as the wrong OS user
For the postgres superuser, switch to the postgres OS account first. This is the intended way to administer a local server.
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo -u postgres psql
sudo -u postgres psql -d mydb -c 'SELECT version();'
sudo -i -u postgres # a shell as postgres, then: psql
Cause 2: an application role without a matching OS user
Your app connects as appuser with a password, which is fine over TCP but not over the socket under peer. The simplest fix is to connect over TCP, where the default rule is password based (scram-sha-256 on current versions).
psql -h localhost -U appuser -d mydb
# or in a connection string
psql 'postgresql://appuser@localhost:5432/mydb'
# DATABASE_URL=postgresql://appuser:secret@localhost:5432/mydb
In an application config, an empty host means the Unix socket; set host=localhost (or 127.0.0.1) explicitly.
Cause 3: you want password login on the socket
Change the method for local connections from peer to scram-sha-256 and reload. Keep the first line for postgres as peer so you can always get in as the OS user.
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo -u postgres psql -c 'SHOW hba_file;'
sudo nano /etc/postgresql/16/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# local all postgres peer
# local all all scram-sha-256
sudo systemctl reload postgresql
# or: sudo -u postgres psql -c 'SELECT pg_reload_conf();'
# The role needs a password set
sudo -u postgres psql -c "ALTER ROLE appuser WITH PASSWORD 'change-me';"
Careful: setting the method to trust lets anyone on the machine log in as any role with no password at all. Use it only for a throwaway local development server, never on a shared host. Also check the role was created with a password that uses the current encryption: SHOW password_encryption; should be scram-sha-256, and roles whose password was stored as md5 need it set again after switching.
Cause 4: map OS users to roles instead
If you like peer authentication but your OS user and role names differ, add a user name map. In pg_hba.conf add map=local to the peer line and define the map in pg_ident.conf.
# pg_hba.conf
local all all peer map=local
# pg_ident.conf
# MAPNAME SYSTEM-USERNAME PG-USERNAME
local alice appuser
local alice postgres
local /^(.*)$ \1
Check that it is fixed
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
psql -h localhost -U appuser -d mydb -c 'SELECT current_user, inet_server_addr();'
sudo -u postgres psql -c 'SELECT 1'
sudo -u postgres psql -c 'TABLE pg_hba_file_rules;' # shows the rules as loaded, with any error
When it is not what you think
password authentication failedis a different method and a wrong password, or a role that has no password set.no pg_hba.conf entry for hostmeans no rule matched at all, typically a TCP connection from an address that is not listed; add ahostline for that network, and make surelisten_addressescovers it.Ident authentication failedis the TCP cousin of peer on RHEL-family defaults; the fix is the same: switch thehostline toscram-sha-256.role "alice" does not existmeans peer succeeded (names matched) but no such database role exists; create it withCREATE ROLE alice LOGINas postgres.- If the error appears in a Docker container, the official image’s
pg_hba.confusestrustlocally andscram-sha-256for the network; peer errors there usually come from the host’s own PostgreSQL, not the container.
Official documentation
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0198 Install PostgreSQL and connect with psql on Debian, Ubuntu and RHEL Install the PostgreSQL server from your distribution or the official PGDG repositories, start it, and open your first psql session as the… Beginner · Step by step · Linux Article Install & connect 15 Oct 2026
- 0199 psql meta-commands cheat sheet The backslash commands you use every day in psql: connecting, listing databases, tables and roles, describing objects, timing queries, expanded output and… Beginner · Cheat sheet · Any system Article Install & connect · Roles & administration 15 Oct 2026
- 0200 PostgreSQL roles, privileges and default privileges Create login and group roles, grant access to databases, schemas and tables, and use ALTER DEFAULT PRIVILEGES so tables created later get… Intermediate · Step by step · Linux Article Roles & administration · Security & access 15 Oct 2026
- 0201 pg_hba.conf and PostgreSQL authentication methods pg_hba.conf decides who may connect from where and how they must prove who they are. Rules are checked top to bottom and… Intermediate · Explains an idea · Linux Article Install & connect · Security & access 15 Oct 2026
- 0209 Fix: FATAL: password authentication failed for user PostgreSQL reached the server and found a matching pg_hba.conf rule, but the password check failed. Usually a wrong password, a role without… Intermediate · Fixes an error · Any system Article Roles & administration · Security & access 15 Oct 2026
- 0210 Fix: FATAL: no pg_hba.conf entry for host The server received the connection but no pg_hba.conf rule matches this client address, user, database and SSL mode. Add or correct a… Intermediate · Fixes an error · Linux Article Install & connect · Security & access 15 Oct 2026
Repos in the wild
- postgres/postgres: PostgreSQL source code mirror postgres/postgres The official read-only GitHub mirror of the PostgreSQL source repository. Browse the code and history here; patches go through the project's mailing lists, not pull requests. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +4,377 chars