skip to Main Content

I am trying to run my app and a postgresql server via docker. I created a docker-compose.yml file that seems to be correct from what I have researched, but postgres is only creating the default postgres user.

I will try to include all relevant files and cli outputs.

docker-compose.yml

version: '3'
services:
  api:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - db
  db:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    env_file:
      - .env
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

.env

POSTGRES_USER="user123"
POSTGRES_PASSWORD="password456"
POSTGRES_DB="db"
POSTGRES_PORT="5432"
POSTGRES_HOST="localhost"
POSTGRES_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=public"

PS > psql -h localhost -U user123

psql: error: FATAL:  role "user123" does not exist

PS > docker exec api_db_1 env

HOSTNAME=31876238fb66
POSTGRES_PORT=5432
POSTGRES_HOST=localhost
POSTGRES_URL=postgresql://user123:password456@localhost:5432/db?schema=public
POSTGRES_USER=user123
POSTGRES_PASSWORD=password456
POSTGRES_DB=db
GOSU_VERSION=1.16
LANG=en_US.utf8
PG_MAJOR=15
PG_VERSION=15.2-1.pgdg110+1
PGDATA=/var/lib/postgresql/data
HOME=/root

postgres=# SELECT usename from pg_user;

 usename
----------
 postgres
(1 row)

2

Answers


  1. The user is only created when Postgres initializes an empty database. If you have an existing database in your volume, the POSTGRES_USER variable isn’t used.

    From the docs:

    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.

    Login or Signup to reply.
  2. It looks like the environment variables from the ‘.env’ file are not being used by the ‘db’ service. You can try to explicitly define the environment variables in the ‘docker-compose.yml’ file under the ‘db’ service:

    version: '3'
    services:
      api:
        build: .
        ports:
          - "8000:8000"
        depends_on:
          - db
      db:
        image: postgres
        restart: always
        ports:
          - "5432:5432"
        environment:
          POSTGRES_USER: user123
          POSTGRES_PASSWORD: password456
          POSTGRES_DB: db
          POSTGRES_PORT: 5432
          POSTGRES_HOST: localhost
        volumes:
          - db-data:/var/lib/postgresql/data
    volumes:
      db-data:
    

    By specifying the environment variables directly in the docker-compose.yml file, you ensure that the db service receives the correct values for the PostgreSQL configuration.

    After making these changes, run the following commands to recreate the containers:

    docker-compose down
    docker-compose up -d
    

    Now, when you try to connect to the PostgreSQL server with the specified user, it should work as expected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search