Docker-compose environment variables

PostgresqlDockerDocker Compose

Postgresql Problem Overview


I am trying to setup a postgres container and want to setup the postgres login with:

POSTGRES_USER: docker
POSTGRES_PASSWORD: docker

So I have created the docker-compose.yml like so

web:
  build: .
  ports:
    - "62576:62576"
  links:
   - redis
   - db
db:
  image: postgres
  environment:
    POSTGRES_PASSWORD: docker
    POSTGRES_USER: docker

redis:
   image: redis

I have also tried the other syntax for environment variable declaring the db section as:

db:
  image: postgres
  environment:
   - POSTGRES_PASSWORD=docker
   - POSTGRES_USER=docker

However neither of these options seem to work because for whatever reason whenever I try to connect to the postgres database using the various connection strings:

postgres://postgres:postgres@db:5432/users
postgres://postgres:docker@db:5432/users
postgres://docker:docker@db:5432/users

They all give me auth failures as opposed to complaining there is no users database.

Postgresql Solutions


Solution 1 - Postgresql

I struggled with this for a while and wasn't having luck with the accepted answer, I finally got it to work by removing the container:

docker-compose rm postgres

And then the volume as well:

docker volume rm myapp_postgres

Then when I did a fresh docker-compose up I saw CREATE ROLE fly by, which I'm assuming is what was missed on the initial up.


The reasons for this are elaborated on here, on the Git repo for the Docker official image for postgres.

Solution 2 - Postgresql

If you're using Docker.

Try to check if your local DB is active because mostly it's conflicting with Docker, if so, you can deactivate it or change port number or uninstall it in order to avoid the conflict.

Solution 3 - Postgresql

The authentication error you got would help a lot!

I fired up the postgres image with your arguments:

docker run --name db -d -e POSTGRES_PASSWORD=docker -e POSTGRES_USER=docker postgres

Then I exec'ed in :

docker exec -it db psql -U docker user
psql: FATAL:  database "user" does not exist

I get the error message you are expecting because I have trust authentication :

docker exec -it db cat /var/lib/postgresql/data/pg_hba.conf | grep -v '^#'

local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host all all 0.0.0.0/0 md5

To simulate your web container, I'll run another instance of the postgres container and link the db container and then connect back to the db container:

core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user
Password for user docker: 
psql: FATAL:  password authentication failed for user "docker"

I get an authentication error if I enter the incorrect password. But, if I enter the correct password:

core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user
Password for user docker: 
psql: FATAL:  database "user" does not exist

It all seems to be working correctly. I put it all in a yaml file and tested it that way as well:

web:
  image: postgres
  command: sleep 999
  ports:
    - "62576:62576"
  links:
   - db
db:
  image: postgres
  environment:
    POSTGRES_PASSWORD: docker
    POSTGRES_USER: docker

then fired it up with docker-compose:

core@ku1 /tmp/i $ docker-compose -f dc.yaml up
Creating i_db_1...
Creating i_web_1...
Attaching to i_db_1, i_web_1
db_1  | ok
db_1  | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
db_1  | initializing pg_authid ... ok
db_1  | initializing dependencies ... ok
db_1  | creating system views ... ok
db_1  | loading system objects' descriptions ... ok
db_1  | creating collations ... ok
db_1  | creating conversions ... ok
db_1  | creating dictionaries ... ok
db_1  | setting privileges on built-in objects ... ok
db_1  | creating information schema ... ok
db_1  | loading PL/pgSQL server-side language ... ok
db_1  | vacuuming database template1 ... ok
db_1  | copying template1 to template0 ... ok
db_1  | copying template1 to postgres ... ok
db_1  | syncing data to disk ... ok
db_1  | 
db_1  | WARNING: enabling "trust" authentication for local connections
db_1  | You can change this by editing pg_hba.conf or using the option -A, or
db_1  | --auth-local and --auth-host, the next time you run initdb.
db_1  | 
db_1  | Success. You can now start the database server using:
db_1  | 
db_1  |     postgres -D /var/lib/postgresql/data
db_1  | or
db_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1  | 
db_1  | 
db_1  | PostgreSQL stand-alone backend 9.4.1
db_1  | backend> statement: CREATE DATABASE "docker" ;
db_1  | 
db_1  | backend> 
db_1  | 
db_1  | PostgreSQL stand-alone backend 9.4.1
db_1  | backend> statement: CREATE USER "docker" WITH SUPERUSER PASSWORD 'docker' ;
db_1  | 
db_1  | backend> 
db_1  | LOG:  database system was shut down at 2015-04-12 22:01:12 UTC
db_1  | LOG:  database system is ready to accept connections
db_1  | LOG:  autovacuum launcher started
^Z
[1]+  Stopped                 docker-compose -f dc.yaml up
core@ku1 /tmp/i $ bg

you can see that the user and password were created. I exec in:

core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user
Password for user docker: 
psql: FATAL:  password authentication failed for user "docker"
core@ku1 /tmp/i $
db_1  | FATAL:  password authentication failed for user "docker"
db_1  | DETAIL:  Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"

core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user
Password for user docker: 
psql: FATAL:  database "user" does not exist
db_1  | FATAL:  database "user" does not exist

So the only thing I can think of is that you are trying to connect to the database from your host, not the web container? Or your web container is not using the 'db' as the host to connect to? Your definition for the web container does not contain any errors that I can see.

Solution 4 - Postgresql

I had the same problem, and in my case problem was fixed with a single command:

docker-compose up --force-recreate

Solution 5 - Postgresql

This helped me

docker stop $(docker ps -qa) && docker system prune -af --volumes && docker compose up

Solution 6 - Postgresql

Thanks to Bryan with the docker-compose exec containername env I have discovered that the need is also to delete volumes. Since for the docker-compose volume rm volumename you need to know the exact name it is easier just to delete all with:

docker-compose down --volumes

Solution 7 - Postgresql

In my case, running postgres:13-alpine in a windows 10 WSL2, none of the above solutions did the trick.

My mistake was that the docker network name I was using was shared with another project. Let's say I have projects A and B, both with the following structure:

myappfolder
  - docker-compose.yml
    - services
        - app
            - depends on db
        - db

It happens that, by default, docker-compose takes the network name from the parent directory name of the docker-compose.yml file. Therefore both projects, A and B were trying to connect to the same network: myappfolder_default.

To solve this:

  1. ensure network names are unique among projects:

    a. either change the name of the root folder to be unique

    b. or edit you docker-compose.yml to set an explicit network name

  2. do docker-compose down -v this will reset all the possible dbs you had defined in your network > make sure you make a psql dump before proceeding

  3. do docker-compose up

More networking docs here: https://docs.docker.com/compose/networking/

Solution 8 - Postgresql

I had a similar situation. Following the answer from @Greg, I did a docker-compose up, and it picked up the environment variable.

Prior to that, I had just been using docker-compose run and it wasn't picking up the environment variable as proven by running docker-compose exec task env. Strangely, docker-compose run task env showed the environment variable I was expecting.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionroyalaidView Question on Stackoverflow
Solution 1 - PostgresqldavetapleyView Answer on Stackoverflow
Solution 2 - PostgresqlAbdelsalam MegahedView Answer on Stackoverflow
Solution 3 - PostgresqlGregView Answer on Stackoverflow
Solution 4 - PostgresqlAlexeyView Answer on Stackoverflow
Solution 5 - PostgresqlAditi LamkhedeView Answer on Stackoverflow
Solution 6 - PostgresqlCrt MoriView Answer on Stackoverflow
Solution 7 - PostgresqlErwolView Answer on Stackoverflow
Solution 8 - PostgresqlBryanView Answer on Stackoverflow