Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

FATAL: password authentication failed for user "…": how to fix it after deploy

FATAL: password authentication failed for user "…" means PostgreSQL was reached but refused the login: the password, or the user name, is not the one the server has. After a deploy it is usually a DATABASE_URL copied from your local .env, a password with special characters that are not percent-encoded in the URL, the wrong user, or a password that was changed. Test the exact value your app reads with psql.

By Livops team, the people who build and run LivopsUpdated

The app was deployed, it starts, and the first query fails. What you see depends on the client, but every one of them carries the same sentence from the server. psql prints its own name, then the connection libpq tried, then the server’s answer[13][12][11][1]:

psql: error: connection to server at "db.example.com" (203.0.113.10), port 5432 failed: FATAL:  password authentication failed for user "app"

Psycopg 3 puts connection failed: in front of the same libpq message[14], and psycopg2 raises it unchanged[15]:

psycopg.OperationalError: connection failed: connection to server at "db.example.com" (203.0.113.10), port 5432 failed: FATAL:  password authentication failed for user "app"
psycopg2.OperationalError: connection to server at "db.example.com" (203.0.113.10), port 5432 failed: FATAL:  password authentication failed for user "app"

node-postgres throws the server’s message itself, with the error code in err.code[19]:

error: password authentication failed for user "app"

Prisma reports it as P1000[21]:

Authentication failed against database server at {database_host}, the provided database credentials for {database_user} are not valid. Please make sure to provide valid database credentials for the database server at {database_host}.

This page explains what the server is telling you, gives the fix for each cause, most common first, and shows how to test the connection with psql before you deploy again.

What does the error mean?

The app got through to PostgreSQL, and the server is willing to talk to it, but not until it passes the authentication method that pg_hba.conf sets for this connection[1]. So the network, the host and the port are fine. It is not the same failure as ECONNREFUSED or Prisma’s P1001, which mean nothing answered at that address: for those, see ECONNREFUSED: app can’t reach its database.

The SQLSTATE code is 28P01, invalid_password[8][9], which is what node-postgres puts in err.code[19].

The message does not always mean the password is wrong. With a password method (scram-sha-256, md5 or password), PostgreSQL sends this one message to the client[9], and when the user does not exist, has no password or has an expired one, it still goes through the motions of authentication so as not to reveal that the user did not exist[9]. The real reason goes only to the server log[9], as a detail line such as[10]:

Role "app" does not exist.
User "app" has no password assigned.
User "app" has an expired password.
Password does not match for user "app".

along with the pg_hba.conf line the connection matched[9]. PostgreSQL’s own advice is the same: the server log may say more about an authentication failure than the client is told[1]. If you can read that log, start there. On a managed database you usually cannot, so work through the causes below.

Causes, most common first

1. The app uses the URL from your local .env

On your laptop the app read DATABASE_URL from .env, pointing at a local server, possibly with a password like postgres. On the server that file is either not there, or it is there and still says the laptop’s password. A local server may never have checked the password at all: with trust in pg_hba.conf, anyone who can connect logs in as any user with no password[4]. So a wrong password in .env worked for months.

When the variable is missing altogether, the client fills in defaults. libpq uses the operating system user name as the user, and the user name as the database[2]; node-postgres defaults to PGUSER=process.env.USER and PGPASSWORD=null[17]. The login then fails for a user you never meant to use. node-postgres may stop even earlier, when the password it was given is not a string, for example undefined from a variable that is not set[20]:

Error: SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string

Fix: read the server’s own value at runtime, and nothing else. Keep .env out of the repository and the upload, set DATABASE_URL (or PGUSER, PGPASSWORD and the rest) in the platform’s variables, and read it in code when the app starts. Before you connect, log the user and host the app is about to use, never the password, and compare them with the database’s.

2. Special characters in the password are not percent-encoded

A PostgreSQL connection URI has the form postgresql://[userspec@][hostspec][/dbname][?paramspec], where userspec is user[:password][2]. If any part contains a symbol with a special meaning in a URI, that part must be percent-encoded[2]. A password with @, /, # or ? in it no longer splits where you expect, and the client may send a different password, or even look for a different host, from the one you meant.

Prisma’s docs say it directly: for PostgreSQL, special characters must be percent-encoded in any part of the connection URL, passwords included, so p@$$w0rd becomes p%40%24%24w0rd[22]. node-postgres decodes the user name and password from the URL with decodeURIComponent[18], so encodeURIComponent is the matching way to encode them.

Fix: encode the password, or keep it out of the URL. In Python, urllib.parse.quote leaves / alone unless you pass safe=""[23]:

from urllib.parse import quote

password = "p@ss/w0rd#1"
url = "postgresql://app:" + quote(password, safe="") + "@db.example.com:5432/app"
# postgresql://app:p%40ss%2Fw0rd%[email protected]:5432/app

In Node.js:

const url = `postgresql://app:${encodeURIComponent(password)}@db.example.com:5432/app`

Or pass the password as its own parameter and skip encoding altogether. Psycopg 3 takes keyword arguments next to the connection string, and they override what the string says[16]. libpq, and so psql and psycopg, read PGPASSWORD as the password parameter[6][16]; node-postgres reads the same variables as libpq[17].

3. The user or the database is not the right one

Because a missing user gets the same message as a wrong password[9], check the user name first: the app may connect as postgres or as your laptop’s user while the database’s login is app. A wrong database name gives a different message, FATAL: database "testdb" does not exist[1], which tells you the user and password were accepted. If no database is given, it defaults to the user name[1].

Fix: take the user and the database from the same source as the password, the database’s own connection details, rather than mixing values from two places.

4. The password was changed, rotated or expired

Somebody set a new password with ALTER ROLE or psql’s \password[3][7], a provider rotated it, or the role’s password has an expiry date that has passed; an expired password fails the same way, with only the server log saying why[10]. A role that has no password at all always fails password authentication[3].

Fix: put the new value everywhere it is used: the app, workers and cron jobs, a migration step, a BI tool. A process that is already running keeps the value it read at start, so restart or redeploy each one after changing it.

5. pg_hba.conf: scram-sha-256 and md5

This one applies only when you run the PostgreSQL server yourself. If the database is managed by a platform, pg_hba.conf is the platform’s, and causes 1 to 4 are the ones to check.

The first pg_hba.conf line that matches the connection type, client address, database and user is used, and if its authentication fails no later line is tried[4]. The method on that line and the way the password is stored must fit together:

  • New passwords are stored as SCRAM-SHA-256 by default, set by password_encryption[5].
  • A password stored as MD5 can be used only with the md5 and password methods[3]. A role whose password was set under md5 years ago fails a scram-sha-256 line until the password is set again.
  • A line with md5 and a password stored as SCRAM is fine: PostgreSQL switches to SCRAM by itself[3].

Fix: with password_encryption = 'scram-sha-256', have each such user set a new password, then change the lines to scram-sha-256; that is PostgreSQL’s own migration order[3]. Changes to pg_hba.conf take effect after pg_ctl reload or SELECT pg_reload_conf()[4]. Do not go the other way: MD5 passwords are deprecated and will be removed in a future release[3][5], and trust needs no password at all[4].

How to test the connection with psql

psql accepts a connection URI where the database name goes[7], so you can test exactly the string your app reads:

psql "$DATABASE_URL"

Quote it, because of the ? and & a URL may contain. Then \conninfo shows which database and user you are connected as[7]. What the result tells you:

  • psql connects, the app does not: the app reads a different value. Print its user and host and compare.
  • psql fails with the URL: test the parts separately, and type the password at the prompt:
psql -h db.example.com -p 5432 -U app -d app -W

-W makes psql ask for the password before connecting[7]. If this works and the URL does not, the URL is encoded wrongly (cause 2). If it fails too, the user or the password itself is wrong (causes 1, 3 and 4).

In scripts and CI, add -w so psql never waits at a prompt and fails at once when it has no password[7]. PGPASSWORD works there too, but PostgreSQL does not recommend it, because on some systems other users can see a process’s environment; it suggests a password file instead[6].

On Livops

  • The database’s address is written to your services. Add PostgreSQL in Databases, and its address is written as variables to every service in the environment, including services added later: DATABASE_URL, plus PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, and POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD and the rest. A URL looks like postgresql://app:<password>@postgres:5432/app. Read it in code; do not paste the DATABASE_URL from your local .env into Variables.
  • Each environment has its own database. staging gets an empty database of its own, and staging’s DATABASE_URL does not carry over to production, so a value copied from one environment fails in the other.
  • New values need a deploy. A changed variable takes effect on the next deploy; to apply it now, restart the service.
  • Testing from your computer. The database has no public port. On the database page, Connect from your computer shows the SSH fields and the database fields, password included. Run the shown ssh -N -L ... command, keep it open, and point psql at localhost with the shown address. Only the project owner sees the password.
  • The read-only login. A readonly login for a BI tool is created on the database page under Overview → Read-only login. New password replaces the old one at once, so a tool still using the old one gets this error until you update it.
  • A failed start keeps the old version. If the app stops at start-up on this error, the deploy fails as start_failed, the message is on the Logs page, and a web app’s previous version keeps running.

A database is free at 500 MB, one per team; 1 GB is 9 credits a month. Everything about the database itself: PostgreSQL hosting and Databases.

Questions

Why does the same DATABASE_URL work on my laptop and fail on the server?

Usually it is not the same value. The server reads whatever is in its own environment, which may be a copy of your local .env, an older password, or nothing at all, in which case the client falls back to defaults such as your operating system user name. Your local server may also accept any password, if its pg_hba.conf uses trust. Print the user and host the app actually uses, without the password, and test that exact value with psql.

Does password authentication failed always mean the password is wrong?

No. With password authentication PostgreSQL sends the same message when the user does not exist, has no password, or has an expired password, so a client cannot tell whether a user name exists. The real reason is written only to the server log.

Which characters in a password need percent-encoding in a connection URL?

Any character with a special meaning in a URL. Prisma's docs give p@$$w0rd, which becomes p%40%24%24w0rd, and PostgreSQL's docs encode an equals sign as %3D and a space as %20. In Python, urllib.parse.quote with safe set to an empty string encodes the slash too; in Node.js, encodeURIComponent is the counterpart of the decodeURIComponent node-postgres applies.

How do I test the connection without my app?

Run psql with the same value the app reads, for example psql followed by the quoted DATABASE_URL. If psql connects and the app does not, the app reads a different value. If psql fails too, connect once with separate options and type the password at the prompt: if that works, the URL is encoded wrongly.

What is the difference between this error and ECONNREFUSED or Prisma P1001?

ECONNREFUSED and P1001 mean the app never reached a PostgreSQL server at that address. Password authentication failed means it did reach one, and the server refused the login. The address is fine; the user name or password is not.

Should I change pg_hba.conf from scram-sha-256 to md5 or trust?

No. trust lets anyone who can connect log in as any user without a password, and MD5 passwords are deprecated. If old roles still have MD5 passwords, set their passwords again with password_encryption at scram-sha-256. On a managed database the file is not yours to edit at all.

Where do I see the database password on Livops?

On the database page, under Connect from your computer, in the database fields. Only the project owner sees the password. Your services do not need it typed in: the database's address, DATABASE_URL and PGPASSWORD included, is written to every service in the environment.

Sources

  1. PostgreSQL docs — Authentication Problems (postgresql.org). Checked .
  2. PostgreSQL docs — Database Connection Control Functions (connection strings) (postgresql.org). Checked .
  3. PostgreSQL docs — Password Authentication (postgresql.org). Checked .
  4. PostgreSQL docs — The pg_hba.conf File (postgresql.org). Checked .
  5. PostgreSQL docs — Connections and Authentication (password_encryption) (postgresql.org). Checked .
  6. PostgreSQL docs — libpq Environment Variables (postgresql.org). Checked .
  7. PostgreSQL docs — psql (postgresql.org). Checked .
  8. PostgreSQL docs — PostgreSQL Error Codes (postgresql.org). Checked .
  9. PostgreSQL source — src/backend/libpq/auth.c (REL_19_STABLE) (raw.githubusercontent.com). Checked .
  10. PostgreSQL source — src/backend/libpq/crypt.c (REL_19_STABLE) (raw.githubusercontent.com). Checked .
  11. PostgreSQL source — src/interfaces/libpq/fe-connect.c (REL_19_STABLE) (raw.githubusercontent.com). Checked .
  12. PostgreSQL source — src/bin/psql/startup.c (REL_19_STABLE) (raw.githubusercontent.com). Checked .
  13. PostgreSQL source — src/common/logging.c (REL_19_STABLE) (raw.githubusercontent.com). Checked .
  14. Psycopg 3 source — psycopg/generators.py (raw.githubusercontent.com). Checked .
  15. Psycopg 2 source — psycopg/connection_int.c (raw.githubusercontent.com). Checked .
  16. Psycopg 3 docs — Connection classes (psycopg.org). Checked .
  17. node-postgres — Connecting (node-postgres.com). Checked .
  18. node-postgres source — pg-connection-string/index.js (raw.githubusercontent.com). Checked .
  19. node-postgres source — pg-protocol/src/parser.ts (raw.githubusercontent.com). Checked .
  20. node-postgres source — pg/lib/crypto/sasl.js (raw.githubusercontent.com). Checked .
  21. Prisma ORM v6 — Error message reference (prisma.io). Checked .
  22. Prisma ORM v6 — Connection URLs (prisma.io). Checked .
  23. Python docs — urllib.parse (docs.python.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy with a database

Got a question? — @livops_uz