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.
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.
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgresRecreate 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.
docker compose down -v && docker compose up -d postgresHow to prevent it
- Set
POSTGRES_PASSWORDon the service, not the step, and reuse it inDATABASE_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.