39

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?

2
  • login permissions are set in the pg_hba.conf file. Commented Aug 26, 2017 at 4:24
  • 2
    Is there a pg_hba.conf file in the shared volume folder ./postgres-data? Commented Aug 26, 2017 at 5:01

7 Answers 7

26

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.

Sign up to request clarification or add additional context in comments.

2 Comments

HI @FrankerZ thanks for your feedback. I just expend to give the relevant information. let me know if you think this answer is more appropriate.
Yup, problem is the volume. I simply changed my volume name and it accepted the new credentials.
19

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"

1 Comment

Why does this matter?
7

Just run

docker compose down -v

then do the

docker compose up

*Warning - First Command will erase your volume

Comments

4

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=# 

Comments

1

Had the same issue. Couldn't solve it for 2 weeks. Read almost everything related to it.

And after I finished all PosgreSQL server related processes on local machine, everything goes well.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
0

Have you deleted the volumes as well?

Try this:

docker stop $(docker ps -qa) && docker rm $(docker ps -qa) && docker volume rm $(docker volume ls -q)

Resource: https://github.com/laradock/laradock/issues/542#issuecomment-271898987

Comments

-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

2 Comments

This is related to Dockerfile, not docker-compose. Downvote.
Please update your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.