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.

Introduction & Quick Summary
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 runPOSTGRES_PASSWORDonly 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
md5hash cannot authenticate whenpg_hba.confdemandsscram-sha-256. - The client is not sending a password. A missing
.pgpass, aPGPASSWORDset 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 error | What it narrows down |
|---|---|
| Both role and client are right | Hash or pg_hba method mismatch |
| Only on one host | Different pg_hba.conf order or hostssl rule |
| Only after a restart | password_encryption changed at server level |
| Only from the app | Connection string or secret not updated |
| Only through port 6543 | Pooler 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:
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_method | Password sent as | Works with SCRAM hash | Works with MD5 hash |
|---|---|---|---|
scram-sha-256 | SCRAM exchange | Yes | No |
md5 | MD5 challenge | No | Yes |
password | Cleartext over the wire | Yes, if client supports | Yes |
trust | Nothing | No password checked | No password checked |
peer | Nothing, OS user used | n/a | n/a |
cert | Client certificate | n/a | n/a |
| Client surface | Where the password comes from | Common mistake |
|---|---|---|
psql | PGPASSWORD, -W prompt, .pgpass | PGPASSWORD exported in another shell |
psql URI | postgresql://user:pass@host/db | Unencoded @, :, /, # |
| JDBC | password= property | Trailing space in the value |
Node pg | password option | Object with password: undefined |
| PgBouncer | auth_file or auth_query | Database role changed, pooler not reloaded |
| Supabase pooler | Project-qualified user | Using 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:
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 firstPOSTGRES_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
[pgbouncer]
auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txtMixing 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:
psql "$DATABASE_URL" -c "select inet_server_addr(), current_user, version()"Common Compilation/Runtime Mistakes & Version Gotchas
md5deprecation. Newer PostgreSQL releases discouragemd5and future versions remove it; plan the SCRAM migration before it is forced by an upgrade.pg_hba.conforder wins. A broadhost all all 0.0.0.0/0 trustabove a specificscram-sha-256line means the password is never checked, while arejectline above the correct one produces this failure with a correct password.- Reload versus restart.
pg_hba.confneeds onlySELECT pg_reload_conf(), butpassword_encryptionchanges 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"andappare different roles; the error prints the role you asked for, so compare it carefully with\du. - IPv6 localhost.
localhostcan resolve to::1, which matches a differentpg_hbaline than127.0.0.1. Test both with-h 127.0.0.1and-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 documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.