Ident authentication failed for user when connecting over TCP
FATAL: Ident authentication failed for user "postgres"Short answer
A host line in pg_hba.conf uses method ident, so the server asked the client machine's ident service which OS user opened the connection, and the answer did not match the database user. Switch that line to scram-sha-256, or add a map, then reload.
Fix it fast
1Find out which cause you have
See which pg_hba.conf line is chosen for a TCP connection. Look for the word ident in the method column.
sudo -u postgres psql -c "SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules"2Match what you see, then jump to its fix
- 1
A line such as host all all 127.0.0.1/32 ident is the first matchThe matching host line uses identFix - 2
The ident service answers, but with a user name that is not the one requestedThe OS user and database user differ and no map existsFix - 3
The file already says scram-sha-256 and the error still names identYou edited pg_hba.conf but the server never re-read itFix
- 1
What does the error look like?
FATAL: Ident authentication failed for user "postgres"psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: Ident authentication failed for user "postgres"
Cause 1 of 3
The matching host line uses ident
1Check
Ident only works on TCP/IP. The server contacts an ident service on the client (RFC 1413, normally TCP port 113) and compares the answer with the database user. Without such a service, or with a different OS user name, it fails.
sudo grep -nE '^\s*host' "$(sudo -u postgres psql -Atc 'SHOW hba_file')"2Fix
Change the method to scram-sha-256 and give the role a password. Use ident only on a closed network where you control every client.
# pg_hba.conf
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
# set a password for the role
sudo -u postgres psql -c "\password postgres"3Verify
Reload, then connect over TCP and enter the password.
sudo -u postgres psql -c "SELECT pg_reload_conf()"
psql -h 127.0.0.1 -U postgres -d postgres -c "select 1"Confirmed by postgresql.org
Cause 2 of 3
The OS user and database user differ and no map exists
1Check
Ident can translate names through a map= option backed by pg_ident.conf. Without one, the names must be identical.
sudo -u postgres psql -c "SHOW ident_file"2Fix
Create a map entry and name it on the host line.
# pg_ident.conf MAPNAME OS-USER DB-USER
webmap www appuser
# pg_hba.conf
host appdb appuser 192.168.1.0/24 ident map=webmap3Verify
Reload and reconnect from that client.
sudo -u postgres psql -c "SELECT pg_reload_conf()"Confirmed by postgresql.org
Cause 3 of 3
You edited pg_hba.conf but the server never re-read it
1Check
The server reads pg_hba.conf at start and on SIGHUP only. Compare what the server loaded with what is on disk.
sudo -u postgres psql -c "SELECT line_number, auth_method, error FROM pg_hba_file_rules"2Fix
Reload the configuration.
sudo -u postgres psql -c "SELECT pg_reload_conf()"3Verify
Repeat the failing connection.
psql -h 127.0.0.1 -U postgres -d postgres -c "select 1"Confirmed by postgresql.org
How do I stop it coming back?
- Prefer scram-sha-256 for every TCP line; use peer only for local socket admin logins.
- Keep pg_hba.conf in version control or configuration management so edits are reviewed.
- Run the pg_hba_file_rules query after each edit and fix any row with an error.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
Peer authentication failed for user, on a local PostgreSQL connection
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"
A pg_hba.conf line with method peer only lets a local socket client in when its OS user name equals the requested database user.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fix