How to Fix PostgreSQL 'FATAL: password authentication failed for user' Error
The FATAL: password authentication failed for user error occurs in PostgreSQL when the database engine rejects a client connection due to an incorrect password, a missing authentication method in pg_hba.conf, or a mismatch between system users and PostgreSQL database roles.
Quick Diagnostics
psql: error: FATAL: password authentication failed for user "postgres": Incorrect user password, missing role, or misconfigured auth method (scram-sha-256, md5, peer) in pg_hba.confpostgres, reset password with ALTER USER, and update pg_hba.confStep-by-Step Solution
-
1
Step 1: Connect via local UNIX socket as the system postgres user
On Linux environments, the default PostgreSQL superuser (
postgres) usespeerauthentication over local UNIX sockets. Access the interactive prompt directly by switching system users:BASH# Switch to the postgres system user and launch psql sudo -u postgres psqlIf the command succeeds and drops you into the
postgres=#prompt, the PostgreSQL daemon is functioning properly; the failure is isolated to TCP/IP password authentication. -
2
Step 2: Reset the PostgreSQL user role password
From the interactive
psqlshell, set a new password for the target database role (such aspostgresor your application user):SQL-- Change password for the postgres database user ALTER USER postgres WITH PASSWORD 'YourNewSecurePassword123!';Exit the interactive terminal using
\q. -
3
Step 3: Configure client authentication in pg_hba.conf
Open PostgreSQL's client authentication configuration file (
pg_hba.conf). You can locate its path by runningSHOW hba_file;insidepsqlor looking in/etc/postgresql/:BASH# Example for PostgreSQL 15/16 on Debian/Ubuntu sudo nano /etc/postgresql/16/main/pg_hba.confInspect access control entries for local and TCP/IP loopback connections (
127.0.0.1/32or::1/128). Ensure they usescram-sha-256ormd5:CONFIG# TYPE DATABASE USER ADDRESS METHOD # Local UNIX socket connections local all postgres peer local all all md5 # IPv4 local connections (TCP/IP) host all all 127.0.0.1/32 scram-sha-256 # IPv6 local connections host all all ::1/128 scram-sha-256 -
4
Step 4: Reload the PostgreSQL service
Reload the PostgreSQL service to apply changes made to
pg_hba.confwithout terminating existing active client connections:BASH# Reload configuration on your Linux server sudo systemctl reload postgresqlTest password authentication via TCP/IP using the
-h localhostparameter:BASHpsql -h localhost -U postgres -W
Prevention Advice
Recommended security practices:
- Enforce
scram-sha-256: Avoid deprecatedmd5authentication on modern PostgreSQL instances (version 13+), asscram-sha-256offers robust protection against replay and dictionary attacks. - Use
.pgpassfiles for automated scripts: Store password credentials securely in a user~/.pgpassfile with0600permissions rather than hardcoding passwords in command lines or bash scripts. - Verify
listen_addressesinpostgresql.conf: If connecting from a remote server, ensurelisten_addresses = '*'is enabled in addition to adding subnets topg_hba.conf.