AlexanderKondrat
AlexanderKondrat

Reputation: 167

Why database is not create when docker-compose up?

i try run PostgreSQL in docker-compose. I can`t create my custom database. For example:

Dockerfile

FROM postgres:14.3
#pg_amqp
WORKDIR /code/pg_amqp-master

COPY ./conf/postgres/pg_amqp-master .

RUN apt update && apt install && apt install make
RUN apt install postgresql-server-dev-14 -y
RUN make && make install

Docker-compose.yml

version: '3.8'

services:
  db:
    container_name: postgres
    build:
      context: .
      dockerfile: conf/postgres/Dockerfile
    volumes:
      - ./conf/postgres/scripts:/docker-entrypoint-initdb.d
      - ./conf/postgres/postgresql.conf:/etc/postgresql/postgresql.conf
      - postgres_data:/var/lib/postgresql/data/
    command: postgres -c config_file=/etc/postgresql/postgresql.conf
    environment:
      - POSTGRES_DB=${DB_NAME}
      - PGUSER=${POSTGRES_USER}
      - PGPASSWORD=${POSTGRES_PASSWORD}
    ports:
      - '5432:5432'
    env_file:
      - ./.env

/scripts/create_extension.sql

create extension if not exists pg_stat_statements;
create extension if not exists amqp;

.env

DB_NAME=mydb
POSTGRES_USER=myuser
POSTGRES_PASSWORD=mypass

When i run docker-compose up -d --build creating is done, but i have one default database - postgres. And all 'create extension' is done in default database - postgres. Where is my mistake?

Upvotes: 0

Views: 1804

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25070

The Postgres environment variables and initialization scripts are only used if Postgres doesn't find an existing database on startup.

Delete your existing database by deleting the postgres_data volume and then start the Postgres container. Then it'll see an empty volume and will create a database for you using your variables and your scripts.

Upvotes: 1

Related Questions