42

According to these docs, I can specify the name of the database created by the postgres docker image with the env var POSTGRES_DB. I have set it in my docker-compose file, but it isn't being created.

Here's relevant section from the compose file:

pg:
    image: postgres:10
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: user-auth
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres

Here are the logs:

Attaching to userauth_pg_1
pg_1   | 2018-02-05 18:05:54.803 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
pg_1   | 2018-02-05 18:05:54.803 UTC [1] LOG:  listening on IPv6 address "::", port 5432
pg_1   | 2018-02-05 18:05:54.806 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_1   | 2018-02-05 18:05:54.817 UTC [24] LOG:  database system was interrupted; last known up at 2018-02-05 18:03:26 UTC
pg_1   | 2018-02-05 18:05:54.942 UTC [24] LOG:  database system was not properly shut down; automatic recovery in progress
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  redo starts at 0/1633ED0
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  invalid record length at 0/1633F08: wanted 24, got 0
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  redo done at 0/1633ED0
pg_1   | 2018-02-05 18:05:54.955 UTC [1] LOG:  database system is ready to accept connections
pg_1   | 2018-02-05 18:05:59.140 UTC [31] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:06:15.528 UTC [32] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:08:46.120 UTC [33] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:08:46.151 UTC [34] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:02.138 UTC [35] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:02.926 UTC [36] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.244 UTC [37] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.273 UTC [38] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.602 UTC [39] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.910 UTC [40] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.777 UTC [41] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.823 UTC [42] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.878 UTC [43] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:06.663 UTC [44] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:06.716 UTC [45] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:16:32.713 UTC [46] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:47:04.603 UTC [47] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:51:34.413 UTC [1] LOG:  received smart shutdown request
pg_1   | 2018-02-05 18:51:34.417 UTC [1] LOG:  worker process: logical replication launcher (PID 30) exited with exit code 1
pg_1   | 2018-02-05 18:51:34.419 UTC [25] LOG:  shutting down
pg_1   | 2018-02-05 18:51:34.434 UTC [1] LOG:  database system is shut down
pg_1   | 2018-02-05 19:08:42.934 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
pg_1   | 2018-02-05 19:08:42.934 UTC [1] LOG:  listening on IPv6 address "::", port 5432
pg_1   | 2018-02-05 19:08:42.937 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_1   | 2018-02-05 19:08:42.951 UTC [25] LOG:  database system was shut down at 2018-02-05 18:51:34 UTC
pg_1   | 2018-02-05 19:08:42.956 UTC [1] LOG:  database system is ready to accept connections
pg_1   | 2018-02-05 19:09:04.316 UTC [32] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 19:09:18.081 UTC [33] FATAL:  database "user-auth" does not exist
6
  • Show us your logs from pg service please, thus we can help you, try docker docker-compose -f Docker-compose.yml logs pg Feb 5, 2018 at 19:16
  • @juliansalas Updated!
    – gardenhead
    Feb 5, 2018 at 19:27
  • Why no try to delete container and create it again? Feb 5, 2018 at 19:33
  • 5
    have you tried removing the volume and starting again?
    – Mazel Tov
    Feb 5, 2018 at 20:45
  • 1
    Can you start with docker-compose down?
    – Shahriar
    Feb 5, 2018 at 20:53

6 Answers 6

83

You most probably started the container without the env variables set. The db was then initialized without user or DB info.

You need to delete the volume and try again.

Credit: https://github.com/docker-library/postgres/issues/453#issuecomment-393939412

3
  • 19
    To find the volume: docker volume ls. To delete the volume: docker volume rm <volume>.
    – kjpc-tech
    Feb 7, 2020 at 22:23
  • 6
    It's important to notice the order of the env vars declared. Needs the be POSTGRES_DB, POSTGRES_USER and POSTGRES_PASSWORD Oct 17, 2021 at 14:31
  • I had to delete existing empty volume and recreated via docker-compose
    – Thobs More
    Feb 15, 2022 at 12:49
2

In my case, I've already had PostgreSQL working in the background on windows. And when I tried to connect to the docker container, I connected to my windows Postgres. You need to open services.msc and turn off PostreSQL

1

You can always initialize scripts for your database before running the container that if even in case you want to add multiple users or create tables initially inside of your container. a basic script would be like :

set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE DATABASE database_name;
    CREATE USER new_user_name WITH ENCRYPTED PASSWORD 'new_password';
    GRANT ALL PRIVILEGES ON DATABASE new_user_name TO database_nam;
EOSQL

and can be saved as a shell file (init_db.sh for example). Make sure at last to copy the file from your relative directory to the container by adding it to the docker-compose.yml file :

services:
  db:
    image: postgres:latest
    ports:
      - '4020:5432'
    environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
    volumes: 
      - ./init_db.sh:/docker-entrypoint-initdb.d/init_db.sh
0

I was using devcontainer (Python 3 & PostgreSQL) in Visual Studio Code and sqltools.connections settings in devcontainer.json were overriding the configuration in docker-compose.yml. I changed the settings there too and it worked as expected!

0

If you are using the Docker application you should delete the existing container and install it again with the command:

docker run --name containerName -p 5432:5432 -e POSTGRES_PASSWORD=postgresql -e POSTGRES_USER=postgresql -e POSTGRES_DB=dbName -d postgres

-1
docker system prune

Adding this removed previous database settings of docker. Then ran again the pipeline after cleanup.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.