Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

FATAL: password authentication failed for user in Postgres

The role exists and the password did not verify. Check the stored hash format, pg_hba order, missing client passwords and pooler credentials.

~5 min readSQL
#PostgreSQL#Authentication#Security#Troubleshooting
A Postgres login gate with a cyan SCRAM hash crate and an amber rejected password key

Introduction & Quick Summary

text
FATAL: password authentication failed for user "app"

PostgreSQL found the role, chose a password-based method from pg_hba.conf, and the supplied password did not verify. Unlike role "app" does not exist, this error confirms the role exists; the failure is in the credential exchange, the hash format, or the client's willingness to send the password at all.

The causes that account for nearly every occurrence:

  • The password is not what you think. A docker run POSTGRES_PASSWORD only takes effect on an empty data directory; changing the env var later changes nothing.
  • The stored hash uses a different algorithm than the server requires. A role created with an md5 hash cannot authenticate when pg_hba.conf demands scram-sha-256.
  • The client is not sending a password. A missing .pgpass, a PGPASSWORD set in a different shell, or a connection string whose special characters were not URL-encoded.
  • You are connecting through a pooler with its own auth. PgBouncer and managed poolers have separate user lists and separate TLS requirements.
  • The superuser name differs. Managed services rename postgres, so the correct password for the wrong role fails identically.
Detail in the errorWhat it narrows down
Both role and client are rightHash or pg_hba method mismatch
Only on one hostDifferent pg_hba.conf order or hostssl rule
Only after a restartpassword_encryption changed at server level
Only from the appConnection string or secret not updated
Only through port 6543Pooler auth, not the database

Core Mechanics, Signatures & Reproducible Setup

Authentication runs in two stages. pg_hba.conf is matched top-down on connection type, database, user and address; the first matching line's method applies, and no fallback happens if it fails. Then that method is executed: scram-sha-256 performs a salted challenge, md5 a double-hash exchange, and password sends cleartext (avoid outside TLS).

Inspect the effective rules and the role's stored hash:

SELECT type, database, user_name, address, auth_method FROM pg_hba_file_rules ORDER BY line_number; SELECT rolname, rolcanlogin, left(rolpassword, 13) AS hash_prefix FROM pg_authid WHERE rolname = 'app';

A SCRAM-SHA-256$ prefix means a modern hash. An md5 prefix means a legacy one, and NULL means no password is set at all.

Fix the credential deliberately:

-- current session (superuser) SET password_encryption = 'scram-sha-256'; ALTER ROLE app WITH PASSWORD 'correct horse battery staple'; -- verify the format that was stored SELECT left(rolpassword, 13) FROM pg_authid WHERE rolname = 'app';

A reproducible server for testing rules is one container with a pinned method:

docker run --name pgtest -e POSTGRES_PASSWORD=secret -p 55432:5432 -d postgres:17 psql "postgresql://postgres:wrong@localhost:55432/postgres" -c 'select 1' # FATAL: password authentication failed for user "postgres"

Then check what the server says it requires:

bash
psql "postgresql://postgres:secret@localhost:55432/postgres" \ -c "select auth_method from pg_hba_file_rules where user_name = array['all']"

Key Comparison Table

auth_methodPassword sent asWorks with SCRAM hashWorks with MD5 hash
scram-sha-256SCRAM exchangeYesNo
md5MD5 challengeNoYes
passwordCleartext over the wireYes, if client supportsYes
trustNothingNo password checkedNo password checked
peerNothing, OS user usedn/an/a
certClient certificaten/an/a
Client surfaceWhere the password comes fromCommon mistake
psqlPGPASSWORD, -W prompt, .pgpassPGPASSWORD exported in another shell
psql URIpostgresql://user:pass@host/dbUnencoded @, :, /, #
JDBCpassword= propertyTrailing space in the value
Node pgpassword optionObject with password: undefined
PgBouncerauth_file or auth_queryDatabase role changed, pooler not reloaded
Supabase poolerProject-qualified userUsing the direct host password on port 6543

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Percent-encoding in a connection URI

A password containing @ or : breaks the URI grammar, so libpq parses the wrong host and user. Encode it first:

python
from urllib.parse import quote uri = f"postgresql://app:{quote('p@ss:w/rd', safe='')}@db:5432/app"

The failure is silent about encoding, so the visible error is still password authentication failed.

Case 2: .pgpass that libpq ignores

printf 'db.internal:5432:app:app:secret\n' > ~/.pgpass chmod 600 ~/.pgpass psql -h db.internal -U app -d app -c 'select current_user'

libpq ignores the file entirely when its mode is broader than 0600, and on Windows the file is %APPDATA%\postgresql\pgpass.conf instead.

Case 3: Docker and the init-only environment variable

docker run -d --name pg -e POSTGRES_PASSWORD=first postgres:17 docker exec pg env | grep POSTGRES_PASSWORD # still first

POSTGRES_PASSWORD is consumed by the entrypoint only when initializing a new data directory. Changing it on an existing volume does nothing; run ALTER ROLE instead.

Case 4: PgBouncer with auth_type = md5 and SCRAM roles

ini
[pgbouncer] auth_type = scram-sha-256 auth_file = /etc/pgbouncer/userlist.txt

Mixing userlist plaintext with SCRAM-backed roles works only when the pooler can perform the exchange. If the pooler is older than the server's algorithm, the app sees a password failure while psql straight to Postgres succeeds, which is the signature of a pooler problem.

Case 5: Pinned password_encryption on a newer server

PostgreSQL 14 and later default to scram-sha-256, so roles created by an older migration script can still hold md5 hashes. After an upgrade, clients that only speak SCRAM fail. Migrate every hash:

SELECT 'ALTER ROLE ' || quote_ident(rolname) || ' WITH PASSWORD NULL;' FROM pg_authid WHERE rolpassword LIKE 'md5%';

Setting a new password is required; there is no in-place conversion. Do it during a window and have every application read the new secret from the same source.

Case 6: Managed services and the wrong host

Pooled endpoints usually require a username like postgres.abcdefghijklmnop, while the direct endpoint uses postgres. Using the direct password on the pooled host, or vice versa, gives exactly this error. Print the effective connection target instead of assuming:

bash
psql "$DATABASE_URL" -c "select inet_server_addr(), current_user, version()"

Common Compilation/Runtime Mistakes & Version Gotchas

  • md5 deprecation. Newer PostgreSQL releases discourage md5 and future versions remove it; plan the SCRAM migration before it is forced by an upgrade.
  • pg_hba.conf order wins. A broad host all all 0.0.0.0/0 trust above a specific scram-sha-256 line means the password is never checked, while a reject line above the correct one produces this failure with a correct password.
  • Reload versus restart. pg_hba.conf needs only SELECT pg_reload_conf(), but password_encryption changes affect only new password writes, not verification of existing hashes.
  • Trailing whitespace in secrets. A secret manager that appends a newline yields a password with \n, which fails uniformly and looks like a wrong password in every client.
  • Case sensitivity of quoted roles. "App" and app are different roles; the error prints the role you asked for, so compare it carefully with \du.
  • IPv6 localhost. localhost can resolve to ::1, which matches a different pg_hba line than 127.0.0.1. Test both with -h 127.0.0.1 and -h ::1.

If the connection settings live in an environment file, inspect it with the env file viewer to catch whitespace and quoting problems before they reach the driver. When the password is embedded in a URI, run it through the URL encoder to see exactly which characters need escaping. To confirm what the server accepts before touching application code, generate a probe request with the curl generator only for HTTP-based admin APIs, and use psql for the database itself.

Primary references: PostgreSQL password authentication explains the methods and hash formats, and the .pgpass documentation covers file location and permissions.

Official references

primary documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.