Skip to content

password authentication failed for user (28P01), Fixed

'password authentication failed for user' (28P01) hides at least seven different causes, and only the server log says which. Wrong server, a role that doesn't exist, a stale .pgpass, an MD5 hash under a SCRAM rule: how to read the DETAIL line and fix the right one.

password-authentication-failed sqlstate-28p01 scram pgpass pg-auth pg-security postgresql postgresql-errors
Gopi Gorantala
Reading Progress

On This Page

You've reset the password. Twice. You copied it out of the vault, pasted it into psql, and got the same password authentication failed for user back. Somebody on the team suggested changing the pg_hba.conf line to trust "just to test", and you're starting to consider it.

Don't. Stop typing passwords for a minute. This error is PostgreSQL being vague on purpose, and the client side will never tell you more than it already has. The real reason is sitting in the server log, one line below the FATAL, and it's usually not "you typed it wrong".

Here's what it looks like

From psql 16.15 (libpq 16.15) against a PostgreSQL 16.15 server, lc_messages = 'C', over TCP:

psql: error: connection to server at "127.0.0.1", port 5446 failed: FATAL:  password authentication failed for user "kyc_app"

Over the Unix socket it's the same FATAL with a different prefix:

psql: error: connection to server on socket "/tmp/lab/.s.PGSQL.5446" failed: FATAL:  password authentication failed for user "kyc_app"

The SQLSTATE is 28P01 (invalid_password in Appendix A of the docs). psql doesn't print it by default; the server log does if your log_line_prefix has %e. Clients older than libpq 14 print a shorter prefix with no "connection to server at" part, but the FATAL text itself has been stable for many major versions. pgJDBC wraps the same server text in org.postgresql.util.PSQLException, and getSQLState() returns 28P01; psycopg raises OperationalError with the same text. A server with lc_messages set to German or Spanish translates the sentence, never the SQLSTATE.

It happens at connect time, before any SQL runs. If it started right after a password rotation, a volume reuse or a new laptop, that already narrows things a lot.

The two-minute triage: ask the server, not the client

Getting this message at all tells you more than it seems. Your client reached a PostgreSQL server (so this isn't Connection refused), and that server found a pg_hba.conf line for your host, user and database (so this isn't no pg_hba.conf entry). The matched line said "ask for a password", and whatever came back didn't pass. If you're not sure which connection error you have, this comparison sorts them by how far the connection got.

What the client never learns is why it didn't pass. So go to the server. On the database host, or with docker logs:

docker logs --tail 20 kyc-pg                 # container
journalctl -u postgresql --since -10min      # systemd on the host
tail -n 20 /var/log/postgresql/postgresql-16-main.log   # Debian/Ubuntu packages

Look for the FATAL line with your timestamp, then read the DETAIL: under it. The table in the fix section maps each DETAIL to its cause. If you don't know where the log lives, that's the first thing to sort out.

And if there's no FATAL line in that log at the time you failed? That's the most useful result of all. You're talking to a different server.

Eight wrong logins in a throwaway cluster

There was no Docker daemon in the sandbox I used, so I ran the lab on Ubuntu 24.04's postgresql-16 package (16.15): two throwaway clusters made with initdb under /tmp, nothing shared. If you have Docker, docker run -d --name kyc-pg -e POSTGRES_PASSWORD=SuperPw1 -p 5446:5432 postgres:19 gets you the same thing, plus docker logs for the server side. Don't run any of this against a database other people use.

Step 1. Create a cluster that uses SCRAM, and log enough to read the failures:

B=/usr/lib/postgresql/16/bin
mkdir -p /tmp/lab && chown postgres /tmp/lab
su postgres -c "$B/initdb -D /tmp/lab/a --auth=scram-sha-256 --pwfile=/dev/stdin -U postgres --locale=C.UTF-8 <<< 'SuperPw1'"
cat >> /tmp/lab/a/postgresql.conf <<'EOF'
port = 5446
listen_addresses = '127.0.0.1'
unix_socket_directories = '/tmp/lab'
lc_messages = 'C'
log_line_prefix = '%m [%p] %q%u@%d %e '
EOF
su postgres -c "$B/pg_ctl -D /tmp/lab/a -l /tmp/lab/a.log start"

Step 2. Make one good role and five broken ones. This KYC document store is the example for the rest of the article:

CREATE ROLE kyc_app LOGIN PASSWORD 'K7#vault@2026';
CREATE DATABASE kyc_docs OWNER kyc_app;
CREATE ROLE kyc_nopw LOGIN;                                          -- no password at all
CREATE ROLE kyc_expired LOGIN PASSWORD 'Exp1red' VALID UNTIL '2020-01-01';
SET password_encryption = 'md5';                                     -- the old hash type, session only
CREATE ROLE kyc_legacy LOGIN PASSWORD 'Legacy1';                     -- stored as an MD5 hash
CREATE ROLE kyc_nologin PASSWORD 'NoLogin1';                         -- NOLOGIN is the default for CREATE ROLE
SELECT rolname, rolcanlogin, left(rolpassword, 14) AS hash_prefix, rolvaliduntil
FROM pg_authid WHERE rolname LIKE 'kyc%' ORDER BY 1;
   rolname   | rolcanlogin |  hash_prefix   |     rolvaliduntil
-------------+-------------+----------------+------------------------
 kyc_app     | t           | SCRAM-SHA-256$ |
 kyc_expired | t           | SCRAM-SHA-256$ | 2020-01-01 00:00:00+01
 kyc_legacy  | t           | md5029a9e8a096 |
 kyc_nologin | f           | md59fc91a09ed9 |
 kyc_nopw    | t           |                |
(5 rows)

(pg_authid needs superuser. pg_roles shows the same roles with the password masked.)

Step 3. Fail on purpose. Wrong password, a typo in the role name, the role with no password, the expired one, and the MD5 one with its correct password:

PGPASSWORD='K7#vault@2025' psql -h 127.0.0.1 -p 5446 -U kyc_app     -d kyc_docs -c 'select 1'
PGPASSWORD='K7#vault@2026' psql -h 127.0.0.1 -p 5446 -U kyc_ap      -d kyc_docs -c 'select 1'
PGPASSWORD='anything'      psql -h 127.0.0.1 -p 5446 -U kyc_nopw    -d kyc_docs -c 'select 1'
PGPASSWORD='Exp1red'       psql -h 127.0.0.1 -p 5446 -U kyc_expired -d kyc_docs -c 'select 1'
PGPASSWORD='Legacy1'       psql -h 127.0.0.1 -p 5446 -U kyc_legacy  -d kyc_docs -c 'select 1'

Expected: five identical-looking FATAL: password authentication failed for user "…" lines on the client, differing only in the user name. That's the confirmation signal. Now the server log for the same five seconds:

2026-12-01 09:42:25.277 CET [352] kyc_app@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_app"
2026-12-01 09:42:25.277 CET [352] kyc_app@kyc_docs 28P01 DETAIL:  Connection matched file "/tmp/lab/a/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2026-12-01 09:42:25.326 CET [354] kyc_ap@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_ap"
2026-12-01 09:42:25.326 CET [354] kyc_ap@kyc_docs 28P01 DETAIL:  Role "kyc_ap" does not exist.
    Connection matched file "/tmp/lab/a/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2026-12-01 09:42:25.379 CET [356] kyc_nopw@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_nopw"
2026-12-01 09:42:25.379 CET [356] kyc_nopw@kyc_docs 28P01 DETAIL:  User "kyc_nopw" has no password assigned.
    Connection matched file "/tmp/lab/a/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2026-12-01 09:42:25.432 CET [358] kyc_expired@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_expired"
2026-12-01 09:42:25.432 CET [358] kyc_expired@kyc_docs 28P01 DETAIL:  User "kyc_expired" has an expired password.
    Connection matched file "/tmp/lab/a/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"
2026-12-01 09:42:25.488 CET [360] kyc_legacy@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_legacy"
2026-12-01 09:42:25.488 CET [360] kyc_legacy@kyc_docs 28P01 DETAIL:  User "kyc_legacy" does not have a valid SCRAM secret.
    Connection matched file "/tmp/lab/a/pg_hba.conf" line 115: "host    all             all             127.0.0.1/32            scram-sha-256"

Step 4. The two that don't involve a typo at all. A stale ~/.pgpass, and a password with a trailing newline (what you get when a Kubernetes secret was made with echo instead of echo -n):

echo '127.0.0.1:5446:kyc_docs:kyc_app:K7#vault@2025' > ~/.pgpass && chmod 600 ~/.pgpass
psql -h 127.0.0.1 -p 5446 -U kyc_app -d kyc_docs -c 'select 1'
PGPASSWORD=$'K7#vault@2026\n' psql -h 127.0.0.1 -p 5446 -U kyc_app -d kyc_docs -c 'select 1'
psql: error: connection to server at "127.0.0.1", port 5446 failed: FATAL:  password authentication failed for user "kyc_app"
password retrieved from file "/root/.pgpass"
psql: error: connection to server at "127.0.0.1", port 5446 failed: FATAL:  password authentication failed for user "kyc_app"

Read that second line of the first failure. libpq tells you the password came from a file you may have forgotten exists. Nobody reads it.

Step 5. Right password, wrong server. Start a second cluster on 5447 (same recipe, port = 5447) holding an old kyc_app with a different password, then point the client at it with the correct password:

psql: error: connection to server at "127.0.0.1", port 5447 failed: FATAL:  password authentication failed for user "kyc_app"

The log of the cluster on 5446, the one you meant, has nothing at that timestamp. The failure is in the other cluster's log. On a laptop, the "other cluster" is usually a Homebrew or Postgres.app server that grabbed 5432 before Docker did.

Teardown: pg_ctl -D /tmp/lab/a stop, the same for b, rm -rf /tmp/lab, and delete the test ~/.pgpass line. With Docker, docker rm -f kyc-pg and remove its volume, or the next lab run will silently keep the old password (that's its own article).

Start here if you're in a hurry

The server asked for a password and what it received didn't verify against the role it looked up. That covers a wrong password, a role that doesn't exist, a role with no password, an expired one, a stored hash the method can't use, and a client that sent a different string than you think. Read the DETAIL line in the server log. It names which.

Why PostgreSQL won't tell you which one it was

This annoyed me until I thought about it. A login prompt that answers "no such user" to one attempt and "wrong password" to another lets anyone on the network list your role names by trying them. So PostgreSQL gives every outsider the same sentence and the same SQLSTATE, and writes the real reason only where an operator can see it. The docs say exactly that in Authentication Problems: the server log may contain more than the client is told. It's deliberate, and the role-that-doesn't-exist case being reported as a password failure is the giveaway.

What happens in order: the postmaster accepts the socket, forks a backend, the backend picks the first matching pg_hba.conf line, and that line names a method. For scram-sha-256, the server looks up the role's stored secret and runs a SCRAM exchange with the client. If there's no role, no secret, a secret of the wrong type, or VALID UNTIL has passed, the server still goes through the motions with a fake secret so timing doesn't leak anything, then fails. The FATAL goes to the client and the DETAIL to the log.

Then the surprise. I expected the plain wrong-password case to say Password does not match for user "kyc_app" in the log, because that's the line every forum thread quotes. Look at the first entry in step 3 again: under SCRAM there's no such DETAIL, only the matched pg_hba.conf line. I switched the rule to md5 and tried a wrong password for the MD5-hashed role:

2026-12-01 09:42:35.597 CET [382] kyc_legacy@kyc_docs 28P01 FATAL:  password authentication failed for user "kyc_legacy"
2026-12-01 09:42:35.597 CET [382] kyc_legacy@kyc_docs 28P01 DETAIL:  Password does not match for user "kyc_legacy".

There it is. So "Password does not match" is an MD5-era message, and on a SCRAM setup a wrong password shows up as the absence of any explanation. That's on 16.15; I didn't run 17, 18 or 19, so check your own version, but the practical rule holds either way: a DETAIL with only the Connection matched line means the role exists, has a usable secret, and the proof didn't match. Wrong string.

One more from the same run. With the md5 rule in place, the SCRAM-hashed kyc_app still logged in with its right password, because an md5 line quietly upgrades to SCRAM when the stored secret is SCRAM. It doesn't work the other way round, which is the kyc_legacy failure above. And kyc_nologin, once I re-set its password as SCRAM, gets a different error once the password checks out: FATAL: role "kyc_nologin" is not permitted to log in, SQLSTATE 28000. Easy to mistake for this one if you only skim.

Reading the DETAIL line and fixing password authentication failed

Match your DETAIL against this table. Each fix assumes you can reach the server as a role that may alter the broken one (superuser, or a role with CREATEROLE in the right relationship to it).

DETAIL in the server log What it means Fix
only Connection matched … line N Role exists, secret usable, the string didn't match Find which string the client actually sent (below)
Role "x" does not exist. Wrong role name, wrong server, or wrong case Check the name and the server; see role does not exist
User "x" has no password assigned. Role was created without PASSWORD ALTER ROLE x PASSWORD … (use \password, below)
User "x" has an expired password. VALID UNTIL is in the past ALTER ROLE x VALID UNTIL 'infinity' or a real future date, then rotate
User "x" does not have a valid SCRAM secret. Stored hash is MD5, rule is scram-sha-256 Re-set the password while password_encryption = 'scram-sha-256'
Password does not match for user "x". MD5 rule, wrong string Same as the first row
nothing logged at that time You reached a different server Find which one (below)

When the string is wrong. libpq picks the password in this order: a password= in the connection string, then PGPASSWORD, then the password file (~/.pgpass, or whatever PGPASSFILE points at), and only then does psql prompt. So whatever you type at the prompt never matters if one of the first three exists. Check them:

env | grep -i ^PG          # a leftover PGPASSWORD or PGPASSFILE from another project
cat ~/.pgpass              # host:port:db:user:password, first matching line wins
psql -W "host=127.0.0.1 port=5446 dbname=kyc_docs user=kyc_app"   # -W forces a prompt; PGPASSWORD still wins if set

The libpq docs recommend against PGPASSWORD anyway, since other users on some systems can read a process's environment. Then check the bytes. Trailing newlines from secret files, a $ that your shell or Docker Compose expanded (Compose treats $VAR in values as interpolation; write $$), and URI special characters all produce this error. In a postgresql:// URI, @, #, /, : and % in the password must be percent-encoded. My unencoded K7#vault@2026 didn't even reach the server; libpq split the URI at the first @ and complained could not translate host name "2026@127.0.0.1". Other clients split differently and send a truncated password instead, which gets you 28P01. Encoded, K7%23vault%402026, it logged straight in. The connection string formats article has the escaping rules per client.

For Java, keep the password in a driver property (spring.datasource.password) rather than the JDBC URL, and remember a SPRING_DATASOURCE_PASSWORD env var in the container beats application.yml. That's the usual reason psql works and pgJDBC fails "with the same password". (Maven Central was unreachable from my sandbox, so no driver output here.)

When it's the wrong server. The log you're watching is silent, so prove where you are. On the database host, ss -ltnp | grep 5432 (or lsof -iTCP:5432 -sTCP:LISTEN on macOS) shows which process owns the port. If two things want 5432 on a Mac, the native server often wins for localhost and your container's published port loses. Log in as a superuser with the address you're using and ask:

SELECT inet_server_port(), current_setting('data_directory'), pg_postmaster_start_time();
 inet_server_port | current_setting | pg_postmaster_start_time
------------------+-----------------+-------------------------------
             5446 | /tmp/lab/a      | 2026-12-01 09:42:12.188367+01
(1 row)

If the data directory isn't the one you expected, stop debugging passwords. Fix the port (publish the container on 5433, or stop the native service) and try again.

When it's case. Role names on the command line and in connection strings are used exactly as written. CREATE ROLE "KycAudit" makes a mixed-case role; -U KycAudit worked in my lab, -U kycaudit failed with Role "kycaudit" does not exist. in the log. Inside SQL, unquoted names fold to lower case, which is how people end up with this mismatch in the first place. Roles, schemas and search_path covers the folding rules.

Resetting a password the safe way. Use psql's \password kyc_app. It hashes on the client and sends only the SCRAM secret, so the plaintext never lands in the server log if someone has log_statement = 'ddl' on. A raw ALTER ROLE kyc_app PASSWORD '…' does end up in that log. Whichever you use, the hash type comes from password_encryption in the session doing the change (default scram-sha-256 since PostgreSQL 14, context user), which is how kyc_legacy got its MD5 hash in my lab.

What isn't a fix: switching the pg_hba.conf method to trust. It "works" because nobody checks a password anymore, for any role on that line, postgres included. Setting password_encryption = md5 to make an old client happy is a step backwards too; MD5 is deprecated as of 18. Upgrade the client instead (an old driver gives a different error, authentication type 10 is not supported).

One password, one source of truth

Most of the causes above come from one habit: the password lives in four places and they drift. A .pgpass from a project two years ago, a PGPASSWORD in .zshrc, a value in application.yml overridden by an env var in the deployment. Then a rotation changes one of them.

What I'd do: the app gets the password from exactly one place per environment, a secret manager or a mounted file, read with no trimming surprises (store it with no trailing newline, and if you must, strip it in code on purpose). No passwords in URLs. No PGPASSWORD in shell profiles. Locally, one ~/.pgpass with lines scoped to host, port and database rather than wildcards, so a stale line can't match a new server. Rotate by creating the new password, deploying, then removing the old login path, not the other way round. And keep every role on SCRAM: run this once and fix anything it lists.

SELECT rolname FROM pg_authid WHERE rolcanlogin AND rolpassword LIKE 'md5%';

On a laptop, publish the container on its own port (-p 5433:5432) so a native install can't shadow it.

What to alert on

Alert when 28P01 for one application user goes above a handful per minute. After a bad rotation that number jumps from zero to hundreds in seconds, and the alert names the service that missed the new secret. Separately, alert on Role "…" does not exist for users you don't recognise, which is somebody guessing. The full set of checks lives in the alerting article.

In CI, the cheapest guard is a smoke test that connects with exactly the credentials the deployment will use, from the same secret, and fails the pipeline on 28P01. It catches the trailing newline, the unencoded # and the forgotten env var before anyone is paged.

Versions to keep in mind. On PostgreSQL 14 and later new passwords default to SCRAM; clusters upgraded from older majors can still hold MD5 hashes. PostgreSQL 18 deprecates MD5 passwords: CREATE ROLE and ALTER ROLE warn when they store one, and the 18 release notes add md5_password_warnings to silence it. A patch posted in February 2026 adds a warning on every successful MD5 login too; check the 19 release notes for whether it shipped in your version. The Connection matched file "…" line N wording in the DETAIL is what 16 prints; it names the file because 16 added include support to pg_hba.conf, and older versions name only the line. All the client-side text above is libpq 16.

What I checked for this one: every password-failure DETAIL I could trigger on 16.15, both hash types under both rule types, a second cluster for the wrong-server case, and libpq's password order. pgJDBC, psycopg and managed services are described, not shown.

Takeaways

  • The client message is the same for wrong password, missing role, expired role and wrong hash type. The server log's DETAIL line isn't.
  • No FATAL in the server log at the time you failed means you reached another server. Check the port's owner before touching passwords.
  • Under SCRAM, a plain wrong password logs no "Password does not match". Only the matched pg_hba.conf line.
  • password= beats PGPASSWORD beats .pgpass beats the prompt. What you type may never be sent.
  • trust isn't a fix, and neither is going back to MD5.

Quick answers

Why does psql work but my Java app gets "password authentication failed"?

Because they aren't sending the same password, or not to the same server. A Spring environment variable such as SPRING_DATASOURCE_PASSWORD overrides application.yml, a URL with an unencoded # or @ gets cut short, and a container's localhost is a different machine from yours. Compare the server log for each attempt: if the Java one doesn't appear there, it went somewhere else.

How do I reset the postgres user password in PostgreSQL?

Connect as a superuser (on most Linux packages, sudo -u postgres psql works over the Unix socket via peer, see peer authentication) and run \password postgres. It sends a SCRAM hash, not the plaintext. In Docker, POSTGRES_PASSWORD only applies to an empty data volume, so changing it later does nothing; use \password inside the container instead.

Is "password authentication failed" the same as "role does not exist"?

From the client's side, yes, when the pg_hba.conf method is a password method: PostgreSQL reports a missing role as a password failure so outsiders can't probe for names. The server log says Role "x" does not exist. in the DETAIL. With peer or trust you get the separate FATAL: role "x" does not exist instead.

What does SQLSTATE 28P01 mean?

28P01 is invalid_password, in class 28, "Invalid Authorization Specification". It's raised by the server during authentication, before your session exists, so there's nothing to roll back; the app should fix its credentials rather than retry in a loop. Reading a PostgreSQL error covers how to get the SQLSTATE out of each driver.

password-authentication-failedsqlstate-28p01scrampgpasspg-authpg-securitypostgresqlpostgresql-errors

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments