Skip to content

Authentication Failed

Error message

                password authentication failed
              

Quick summary

The database server refused your login because the username, password, or authentication method does not match.

Why this happens

  • Wrong password (typo or changed password)

  • Wrong username

  • The user does not have permission to connect to this database

  • Authentication method mismatch (e.g., md5 vs scram-sha-256)

  • The user does not exist in the database

Minimal example

✗ Broken code
# Wrong password
psql -U postgres -h localhost
# → password authentication failed for user "postgres"
✓ Fixed code
# Correct password
PGPASSWORD=correct_password psql -U postgres -h localhost

The password was wrong. Providing the correct password (via PGPASSWORD env var) fixes the error.

How to diagnose

  • Verify the username and password are correct

  • Check if the user exists: \du in psql or SELECT * FROM pg_user

  • Verify the user has permission to connect to the database

  • Check the authentication method in the server configuration

How to fix

  • Correct the username and password in your connection string

  • Create the user if it does not exist: CREATE USER name WITH PASSWORD 'pass'

  • Grant connect permission: GRANT CONNECT ON DATABASE db TO user

  • Update the authentication method in pg_hba.conf if needed

How to prevent

  • Store credentials in environment variables or a secrets manager

  • Use connection pooling with validated credentials

  • Document database access requirements for new team members

Related Resources

Related Glossary

Related Lessons

← Back to language