0

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?

1 Answer 1

1

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.

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

1 Comment

Before i run docker-compose up, i was delete all containers, volumes and images using postgres. but it`s still not work(

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.