'Can't set Password for Postgres using docker-compose
I am not able to set Password for Postgres using Docker-compose. Postgres is loading without password and with the default user name "postgres", non of the environment variables below seems to applied. below is the db service of my docker-compose.yml file: (version 3)
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"
Note I tried using the "-POSTGRES_USER=" as well, it didn't work
Also, I deleted all old containers/volumes.
Any idea?
Solution 1:[1]
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.
Solution 2:[2]
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"
Solution 3:[3]
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 db
Password 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=#
Solution 4:[4]
Start postgres instance:-
docker run --name postgres-0 -e POSTGRES_PASSWORD=mypassword -p 5433:5433 -d postgres
Now we can check docker all running container by this command:-
docker ps
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | larsks |
Solution 4 | user16057252 |