Skip to content
FR
  1. LuzineDebug
  2. Databases (SQL)
  3. FATAL: password authentication failed for user…
Databases (SQL) Intermediate ≈ 15 min to fix

Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)

Error message
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"
In short

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.

Be the first to confirm this fix

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

  1. postgres role with no password: on Linux (apt/dnf packages) it is only reachable through peer via sudo -u postgres psql; any TCP connection with a password fails.
  2. Docker volume already initialized: POSTGRES_PASSWORD is only applied when the cluster is created, on an empty volume. Changing it later does nothing.
  3. Wrong server: a local PostgreSQL (Homebrew, Postgres.app, Windows service) already listens on 5432 and answers instead of the container.
  4. md5 hash with a scram-sha-256 line: password created before PostgreSQL 14 (or with password_encryption = md5) then pg_hba.conf switched to scram-sha-256. A too-old client (libpq < 10) can also fail against SCRAM.
  5. Special characters in the URL: in DATABASE_URL, an @, : or / in the password must be encoded (%40, %3A, %2F).

Step-by-step fix

  1. 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).

    bash
    sudo lsof -nP -iTCP:5432 -sTCP:LISTEN
    psql "host=127.0.0.1 port=5432 user=postgres dbname=postgres"
  2. Log in locally and set the password

    On Linux, peer authentication lets you in without a password as the postgres system user. \password prompts for the new password without writing it to the history.

    bash
    sudo -u postgres psql
    \password postgres
  3. 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 -v would also reset the password, but by deleting all data.

    bash
    docker compose exec db psql -U postgres -c "ALTER ROLE postgres WITH PASSWORD 'A-Long-Random-Password-42';"
  4. Check pg_hba.conf

    Find the file and the loaded rules. Add a host line for your network with scram-sha-256, then reload the configuration. Only use trust temporarily on a local machine: it lets anyone in without a password.

    sql
    SHOW hba_file;
    SELECT line_number, type, database, user_name, address, auth_method FROM pg_hba_file_rules;
    SELECT pg_reload_conf();
  5. 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.

    plaintext
    # 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
  6. Convert an old md5 hash to SCRAM

    If rolpassword starts with md5, set the password again with password_encryption = 'scram-sha-256'. Also update old drivers (libpq 10+, a recent pg for Node.js).

    sql
    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';
  7. 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).

    bash
    # 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 using postgres.
  • 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

All Databases (SQL) errors →