Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Reset the postgres password when you cannot log in to PostgreSQL

ErrorFATAL: 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.

  • Applies to PostgreSQL 16, PostgreSQL 17, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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';"
  2. 2Match what you see, then jump to its fix

    1. 1sudo -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. 2Even 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. 3The 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

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 postgres

3Verify

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

Did this fix it?

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 reload

3Verify

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

Did this fix it?

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 postgres

3Verify

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

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.