14

Actually I'm using the following docker-compose.yml file

version: '3.3'

  services:
    postgres:
      container_name: postgres
      image: postgres:latest
      restart: always
      environment:
        POSTGRES_USER: ${POSTGRES_USER}
        POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
        POSTGRES_DB: ${POSTGRES_DB}
        PGDATA: /var/lib/postgresql/data/pgdata
      ports:
        - "5432:5432"
      volumes:
        - ./data/postgres/pgdata:/var/lib/postgresql/data/pgdata

I use also this .env file in the same directory of the docker-compose.yml file:

POSTGRES_USER=dbadm
POSTGRES_PASSWORD=dbpwd
POSTGRES_DB=db

Then I run a bash shell into container this way:

docker exec -ti postgres bash

And after this invoke the command:

psql -h postgres -U dbadm db

And I get the error:

psql: FATAL:  password authentication failed for user "dbadm"

The strange fact is that if use the default image parameters:

psql -h postgres -U admin database

And insert the default password "password", it logs me in, and seems it's ignoring the environment variables.

What am I missing?

Additional logs from docker-compose up logs:

postgres    | 2017-10-15 09:19:15.502 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres    | 2017-10-15 09:19:15.502 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres    | 2017-10-15 09:19:15.505 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres    | 2017-10-15 09:19:15.524 UTC [22] LOG:  database system was shut down at 2017-10-15 09:02:21 UTC
postgres    | 2017-10-15 09:19:15.530 UTC [1] LOG:  database system is ready to accept connections

Cannot see any "RUN" line about user, database and password setup.

16
  • It is confusing, you are trying to login as dbadm and it doesn't work but then you are saying that you can login as admin. Have you tried using the admin user in your .env file? Commented Oct 15, 2017 at 9:08
  • Thanks for the interest @Sergiu as I said it works using the default values from the image but it's not my problem. Commented Oct 15, 2017 at 9:13
  • Could you please run this env | grep POSTGR inside your postgres container? Commented Oct 15, 2017 at 9:28
  • Did you try to specify values directly (POSTGRES_USER: dbadm, etc)? Not sure, but maybe you also need to add env_file: .env to your config as mentioned in docs: docs.docker.com/compose/compose-file/#env_file Commented Oct 15, 2017 at 9:41
  • Another thing to try: shift environment section forward, to place it under postgres instead of having it on the services level. Commented Oct 15, 2017 at 9:52

2 Answers 2

11

according to https://hub.docker.com/_/postgres documentation.

Warning: 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.

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

Comments

3

I have created a docker-compose yml and .env file with the details you've provided and everything works fine as you can see from the pictures below:

enter image description here enter image description here

I think your problem lies when you are passing the -h parameter.

Inside the container will always be localhost however, outside you will have to pass:

hostname: postgres

inside your docker-compose file so it will have the postgres hostname

3 Comments

Using localhost it allows local connections and skip totally the user created. The problem is on how I'm mounting the directory for data. @sergiu
Yes that's right, you are only using local connections, however, in your /etc/hosts file you will have to add the container IP and postgres, as follows: 0.0.0.0 postgres and then try to connect
Fine it seems that setting the env variable PGDATA creates a volume in wich postgres stores db binaries and configuration files (also accesses). Then I mount a volume that I want to be on the host for simplicity during local development. But mounting the volume will override all it seems. Am I wrong?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.