Reset the postgres password when you cannot log in to PostgreSQL
FATAL: password authentication failed for user "postgres"Short answer
You do not need the old password. If you have root or sudo on the server, open psql as the postgres OS user over the local socket and run \password postgres. If the socket login is blocked too, switch the local rule to trust for a moment, reload, set the password, then switch it back and reload.
Fix it fast
1Find out which cause you have
Check which rule applies to local connections. If the first matching line says peer, the OS account route in the first cause works.
sudo -u postgres psql -c "SELECT * FROM pg_hba_file_rules WHERE type = 'local';"2Match what you see, then jump to its fix
- 1
sudo -u postgres psql opens a prompt without asking for a passwordThe local socket rule is peer, and you can use the postgres OS accountFix - 2
Even sudo -u postgres psql is refused, or no local rule lets the postgres user inThe socket login is blocked as well, so the rule has to change for a momentFix - 3
The new password is rejected on a scram-sha-256 line even though \password succeededThe application uses a password for TCP, but the hash type does not match the ruleFix
- 1
What does the error look like?
FATAL: password authentication failed for user "postgres"The postgres password is lost or was never set, and no application can log in as postgres
Cause 1 of 3
The local socket rule is peer, and you can use the postgres OS account
1Check
The peer method matches the client's operating system user name against the database user name. It works only for local connections, so root or sudo access to the server is the key.
sudo -u postgres psql -c "select current_user"2Fix
Use \password, which asks for the new password and sends it encrypted, so it is not left in history or logs.
sudo -u postgres psql
\password postgres3Verify
A TCP login with the new password works.
psql "host=127.0.0.1 dbname=postgres user=postgres" -c "select 1"Confirmed by postgresql.org
Cause 2 of 3
The socket login is blocked as well, so the rule has to change for a moment
1Check
The first pg_hba.conf record that matches decides, and there is no fall-through. Find the file the server uses; it lives in the data directory unless hba_file moves it.
sudo -u postgres psql -c "SHOW hba_file;"2Fix
As a temporary measure, put a trust line for local connections above the other local lines, reload, set the password, then remove the line and reload again. trust lets anyone who can connect log in as any user, so do not leave it in place, and keep the machine closed to other users meanwhile.
# pg_hba.conf, temporary first local line
local all postgres trust
# after editing
sudo -u postgres pg_ctl -D /path/to/data reload3Verify
Set the password with \password postgres, restore the original line, reload, and confirm the rules.
SELECT * FROM pg_hba_file_rules;Confirmed by postgresql.org
Cause 3 of 3
The application uses a password for TCP, but the hash type does not match the rule
1Check
The stored hash is created with the password_encryption setting at the time the password is set. An MD5 hash cannot be used with a scram-sha-256 rule.
SHOW password_encryption;2Fix
Set password_encryption to scram-sha-256, reload, and run \password postgres again.
SELECT pg_reload_conf();
\password postgres3Verify
Log in again over TCP with the new password.
psql "host=127.0.0.1 user=postgres dbname=postgres" -c "select 1"Confirmed by postgresql.org
How do I stop it coming back?
- Keep the peer rule for the postgres account and use separate roles with passwords for applications.
- Never leave a trust line in pg_hba.conf, and keep the file in version-controlled configuration management.
- Store the new password in a password manager and not in shell history.
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
Password authentication failed right after switching pg_hba.conf from md5 to scram-sha-256
pg_hba.conf line changed from md5 to scram-sha-256, and every login with a password now fails
A password stored as an MD5 hash cannot be used with the scram-sha-256 method.
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 fix