psql: FATAL: PostgreSQL password authentication failed for user “username”

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
PostgreSQL password authentication failed

psql: FATAL: PostgreSQL password authentication failed for user “username”

Error :

psql: FATAL: password authentication failed for user “username”

🚨 Fixing the PostgreSQL Password Authentication Failed Error

If you are working as a DBA or developer, one of the most common issues you will face is the PostgreSQL password authentication failed error. This usually occurs when PostgreSQL rejects the credentials provided by the client. The full error looks like:

Why Does This Error Happen?

The error can occur due to several reasons:

  • The PostgreSQL user does not exist.

  • The password entered is incorrect.

  • The authentication method in pg_hba.conf is not configured correctly.

  • The connection is pointing to the wrong database or host.

Solution:
1 . Login as a superuser (e.g., postgres):
\du

If username is missing, create it:

CREATE ROLE username LOGIN PASSWORD 'your_password';
2 . Reset password
ALTER ROLE username WITH PASSWORD 'new_password';
3 . Check the database you’re connecting to

Make sure you’re pointing to the right DB:

psql -U username -d dbname -h localhost -p 5432

If DB doesn’t exist:

CREATE DATABASE dbname OWNER username;
4 . Verify pg_hba.conf

Open pg_hba.conf (location: usually /etc/postgresql/<version>/main/pg_hba.conf or $PGDATA/pg_hba.conf).

Look for a line like:

# TYPE   DATABASE   USER   ADDRESS       METHOD
  host   all        all    127.0.0.1/32  md5
  • If it says peer instead of md5 or scram-sha-256, local password logins may fail.

  • Change it to:

host    all    all    127.0.0.1/32   scram-sha-256

Then reload:

sudo systemctl reload postgresql
5. Test connection
psql -h localhost -U username -d dbname

If it still fails, try forcing password prompt:

psql -h localhost -U username -d dbname -W

Conclusion

 At Learnomate Technologies, we’re here to support you every step of the way with top-notch training in PostgreSQL DBA and more.

For more detailed insights and tutorials, do check out our YouTube channelwww.youtube.com/@learnomate, where we regularly share practical tips and deep dives into essential database topics. And if you’re serious about mastering PostgreSQL DBA, head over to our website for our full training program: learnomate.org/training/postgresql-training/.

I’d love to connect with you, so don’t forget to follow my LinkedInhttps://www.linkedin.com/in/ankushthavali/. If you’re eager to read more about various technologies, explore our blog page here: https://learnomate.org/blogs/. Happy learning, and remember—tuning and training go hand-in-hand for database success!