Can't set Password for Postgres using docker-compose
The problem should be with the volume attached. When your container start it will add the credentials you give him, but then the volume will be attached and that will cause this information being rewritten.
For more information have a look at https://github.com/docker-library/postgres/issues/203#issuecomment-255200501.
The main reason being use of ':' instead of "=" in the environment section.Ideally it should look like this:
db: image: postgres container_name: postgres environment: - POSTGRES_USER=user - POSTGRES_PASSWORD= pass - POSTGRES_DB= db restart: unless-stopped volumes: - ./postgres-data:/var/lib/postgresql/data ports: - "5432:5432"
Although very late to this but I hope this helps someone who comes looking for a similar solution.
Your configuration works fine for me. I suspect you are not using the complete set of correct credentials, which includes the username, password, and database name. If I take your example docker-compose.yaml
and run it without modifications, I can connect to the database db
like this with username user
and password pass
:
$ psql -h localhost -U user dbPassword for user user: psql (9.5.7, server 9.6.1)WARNING: psql major version 9.5, server major version 9.6. Some psql features might not work.Type "help" for help.db=#