Home > Enterprise >  Docker-compose postgres cant init my tables in newly created database
Docker-compose postgres cant init my tables in newly created database

Time:05-24

I am trying to automate the INIT process in Docker so whenever I docker-compose up in an empty data directory it will create two DBS 1st one is Airflow and the second one is my test DB so for instance when I docker-compose up Postgres run the first SQL file in my new test DB to create tables. However, it didn't run the insert SQL files I have already mounted in docker-entry point what is does it run those files in the Airflow database can anyone tell me how to insert all metadata tables in my Test Database instead of airflow one thanks

My docker-compose file

  postgres:
    image: postgres:13

    environment:
      POSTGRES_USER: airflow
      POSTGRES_PASSWORD: airflow
      POSTGRES_MULTIPLE_DATABASES: airflow ,docker_airflow

    ports: 
    - "5432:5432"

    volumes:
      - postgres-db-volume:/var/lib/postgresql/data
      - ../data-platform/db/metadata/init/1._ddl.sql:/docker-entrypoint-initdb.d/1._ddl.sql
      - ../data-platform/db/metadata/init/2._init.sql:/docker-entrypoint-initdb.d/2._init.sql
      - ../data-platform/db/metadata/init/3._props.sql:/docker-entrypoint-initdb.d/3._props.sql
      - ../data-platform/db/metadata/init/4.ge_ddl.sql:/docker-entrypoint-initdb.d/4.ge_ddl.sql
      - ../data-platform/db/metadata/init/5.ge_init.sql:/docker-entrypoint-initdb.d/5.ge_init.sql
      - ../data-platform/db/metadata/init/6._be_lu_nl.sql:/docker-entrypoint-initdb.d/6._be_lu_nl.sql
      - ../data-platform/db/metadata/init/7._registration_nl_lifecycle.sql:/docker-entrypoint-initdb.d/7._registration_nl_lifecycle.sql

    healthcheck:
      test: ["CMD", "pg_isready", "-U", "airflow"]
      interval: 5s
      retries: 5
    restart: always

CodePudding user response:

When running the scripts you need to set each one up with the correct information. They’re run with the default credentials which means they also connect to the default database.

You can add to the beginning of each file instructions for psql to connect to the desired database with \c somedb. It needs to be in each script since they’re run separately and won’t hold any information between runs.

  • Related