Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)
FATAL: password authentication failed for user "postgres"
- psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "postgres"
- SQLSTATE[08006] [7] connection to server at "127.0.0.1", port 5432 failed: FATAL: password authentication failed for user "laravel"
- psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"
- FATAL: no pg_hba.conf entry for host "172.18.0.1", user "app", database "app", no encryption
- error: password authentication failed for user "postgres"
PostgreSQL received the connection but the password does not match the role, or the role has no password. Log in locally with sudo -u postgres psql, set the password with \password, then check in pg_hba.conf that the line matching your connection uses scram-sha-256.
What this error means
For each connection, PostgreSQL reads pg_hba.conf top to bottom and takes the first line matching the type (local for the Unix socket, host for TCP), database, role and address. That line sets the method: peer (system user name), scram-sha-256 or md5 (password), trust (no check). With a password method, the server compares what the client sends with the hash stored in pg_authid. If it does not match, if the role has no password, or if the hash is md5 while the line requires scram-sha-256, the connection is refused with this FATAL message (SQLSTATE 28P01).
Common causes
postgresrole with no password: on Linux (apt/dnf packages) it is only reachable throughpeerviasudo -u postgres psql; any TCP connection with a password fails.- Docker volume already initialized:
POSTGRES_PASSWORDis only applied when the cluster is created, on an empty volume. Changing it later does nothing. - Wrong server: a local PostgreSQL (Homebrew, Postgres.app, Windows service) already listens on 5432 and answers instead of the container.
- md5 hash with a scram-sha-256 line: password created before PostgreSQL 14 (or with
password_encryption = md5) thenpg_hba.confswitched toscram-sha-256. A too-old client (libpq < 10) can also fail against SCRAM. - Special characters in the URL: in
DATABASE_URL, an@,:or/in the password must be encoded (%40,%3A,%2F).
Step-by-step fix
-
Check which server you are talking to
See which process listens on 5432 (macOS/Linux); on Windows,
Get-NetTCPConnection -LocalPort 5432. If it is a local PostgreSQL rather than Docker, stop it or publish the container on another port (5433:5432).sudo lsof -nP -iTCP:5432 -sTCP:LISTEN psql "host=127.0.0.1 port=5432 user=postgres dbname=postgres" -
Log in locally and set the password
On Linux,
peerauthentication lets you in without a password as thepostgressystem user.\passwordprompts for the new password without writing it to the history.sudo -u postgres psql \password postgres -
Docker: change the password inside the container
In the official image, local connections inside the container use
trust: you can change the role without knowing the old password. Then put the same value in the app configuration.docker compose down -vwould also reset the password, but by deleting all data.docker compose exec db psql -U postgres -c "ALTER ROLE postgres WITH PASSWORD 'A-Long-Random-Password-42';" -
Check pg_hba.conf
Find the file and the loaded rules. Add a
hostline for your network withscram-sha-256, then reload the configuration. Only usetrusttemporarily on a local machine: it lets anyone in without a password.SHOW hba_file; SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules; SELECT pg_reload_conf(); -
Example pg_hba.conf rules
Order matters: the first matching line wins. Restrict the address to the app’s network rather than
0.0.0.0/0.# TYPE DATABASE USER ADDRESS METHOD local all all peer host all all 127.0.0.1/32 scram-sha-256 host all all ::1/128 scram-sha-256 host app app 172.18.0.0/16 scram-sha-256 -
Convert an old md5 hash to SCRAM
If
rolpasswordstarts withmd5, set the password again withpassword_encryption = 'scram-sha-256'. Also update old drivers (libpq 10+, a recent pg for Node.js).SELECT rolname, left(rolpassword, 14) FROM pg_authid WHERE rolname = 'app'; SET password_encryption = 'scram-sha-256'; ALTER ROLE app WITH PASSWORD 'A-Long-Random-Password-42'; -
Encode the password in the URL
In a connection string, percent-encode reserved characters in the password. With Laravel, then clear the config cache (
php artisan config:clear).# password: p@ss:word DATABASE_URL=postgresql://app:p%40ss%[email protected]:5432/app
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Create a dedicated role per application (
CREATE ROLE app LOGIN PASSWORD '...') instead of usingpostgres. - In Docker, publish PostgreSQL on a separate port (
5433:5432) if a local PostgreSQL is installed, and document it in.env.example. - Keep
password_encryption = scram-sha-256(default since PostgreSQL 14) and up-to-date drivers.
Frequently asked questions
What is the default password of the postgres user?
There is none. On Linux you connect with sudo -u postgres psql; in Docker it is the value of POSTGRES_PASSWORD when the volume was first started.
I changed POSTGRES_PASSWORD in docker-compose and it does not work, why?
The variable is only read if the data directory is empty. With an existing volume, change the password with ALTER ROLE inside the container.
What is the difference between "password authentication failed" and "Peer authentication failed"?
The first concerns a password method (scram-sha-256, md5). The second comes from a peer line: your system user name does not match the role. Use sudo -u postgres or connect over TCP with -h 127.0.0.1.
Useful tools for this error
More Databases (SQL) errors
- Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)
- Fix "ERROR 1062 (23000): Duplicate entry for key" (MySQL, MariaDB, PostgreSQL)
- Fix "Deadlock found when trying to get lock; try restarting transaction" (MySQL, PostgreSQL)
- Fix "ERROR 1040: Too many connections" (MySQL) and "too many clients already" (PostgreSQL)
- Fix "ERROR 2006 (HY000): MySQL server has gone away"
- Fix "ERROR 1054 (42S22): Unknown column in 'field list'" (MySQL)
- Fix "ERROR: relation "users" does not exist" (PostgreSQL)