skip to Main Content

It seems to me that Postgres is ignoring my envar and the sql script

Here’s my docker-compose

version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: mysecretpassword
      PGDATA: /var/lib/postgresql/data/pgdata
      POSTGRES_DB: ecommerce
    volumes:
      - /custom/mount:/var/lib/postgresql/data
      - ./sql_script.sql:/docker-entrypoint-initdb.d/sql_script.sql
    ports:
      - 5432:5432

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

In the doc is saying that I can change the default database name using POSTGRES_DB env var but is not working for me. At first my intention was to create the database using a sql_script.sql through volumes, got it from the doc as well but didn’t work.

Thanks in advance.

2

Answers


  1. The POSTGRES_DB variable is only used if your /custom/mount directory is empty. If it already contains a database, the 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.

    Delete the contents of the directory and run your container again, and it should create the database.

    Login or Signup to reply.
  2. If you want to use the POSTGRES_DB variable to create a new database even if the /custom/mount directory already contains a database, you can modify the startup process of the PostgreSQL container. PGSQL Docker Documentation

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