Skip to content
Latchkey

PostgreSQL "FATAL: password authentication failed" from wrong env in CI

Postgres rejected the login because the password in your connection string does not match the one the container was initialized with. The fix is making POSTGRES_PASSWORD and DATABASE_URL agree.

What this error means

Authentication fails with "FATAL: password authentication failed for user \"postgres\"" while the container itself is healthy and reachable.

Terminal
psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed:
FATAL:  password authentication failed for user "postgres"

Common causes

The connection password does not match POSTGRES_PASSWORD

The image sets the superuser password from POSTGRES_PASSWORD only on first init. Your DATABASE_URL sends a different password, so login fails.

The env var is set on the step but not on the service

Setting the password in the job step env does not configure the container. It must be under the service env, and the step must use the same value.

How to fix it

Set the password once and reuse it

Define the password on the service and build the connection string from the same value.

.github/workflows/ci.yml
services:
  postgres:
    image: postgres:16
    env:
      POSTGRES_PASSWORD: postgres
    ports: ['5432:5432']
env:
  DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres

Recreate the volume if the password changed

The password is only applied when the data directory is empty. If you changed it but reused a volume, remove the volume so init runs again.

Terminal
docker compose down -v && docker compose up -d postgres

How to prevent it

  • Set POSTGRES_PASSWORD on the service, not the step, and reuse it in DATABASE_URL.
  • Remember the password applies only on first initialization of an empty data dir.
  • Keep the password in one place (a secret or a single env) to avoid drift.

Frequently asked questions

What causes "Postgres "password authentication failed""?
The image sets the superuser password from POSTGRES_PASSWORD only on first init. Your DATABASE_URL sends a different password, so login fails.
How do I fix Postgres "password authentication failed"?
Define the password on the service and build the connection string from the same value.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card