skip to Main Content

I’m trying to build a docker-compose to set up a postgres database with an initial script that creates some tables there.

I have this docker-compose.yml

version: '3.9'
services:
    postgres:
        image: postgres:12.7
        #restart: always
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
        logging:
          options:
            max-size: 10m
            max-file: "3"
        ports:
          - '5438:5432'
        volumes: 
          - ./postgres-data:/var/lib/postgresql/data
          # copy the sql script to create tables
          #- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
          - ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh

this is the create_tables.sh:

#!/bin/bash          
echo Start Executing SQL commands
psql -U postgres -f create_tables.sql

And the error log:

postgres_1 | psql: error: create_tables.sql: No such file or directory 

Uncommenting the line - ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql and commenting the line - ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh make it work but I have to use the .sh.

My dockerfile:

FROM postgres:12.7

#WORKDIR ./sql/

ADD ./sql/create_tables.sql ./docker-entrypoint-initdb.d/create_tables.sql

ADD ./sql/create_tables.sh ./docker-entrypoint-initdb.d/create_tables.sh

RUN ./docker-entrypoint-initdb.d/create_tables.sh

Any ideas what I’m doing wrong? Thanks!!

2

Answers


  1. Chosen as BEST ANSWER

    Finally I separate a sql folder and a sh folder and it worked, but the correct approach would be Paulo answer.

     - ./db-init-scripts/create_tables.sh:/docker-entrypoint-initdb.d/init.sh 
     - ./sql:/sql
    

  2. You just need to use this script below, pointing to your ".SQL" files folder in the volumes step.
    It will automatically create the database and execute each SQL file inside this folder. You don’t need a separate .sh file to migrate/create/execute your SQL commands using the docker-compose with the correct volume command.

    version: '3.9'
    services:
        postgres:
            image: postgres:12.7
            #restart: always
            environment:
              - POSTGRES_USER=postgres
              - POSTGRES_PASSWORD=postgres
            logging:
              options:
                max-size: 10m
                max-file: "3"
            ports:
              - '5438:5432'
            volumes: 
              - ./postgres-data:/var/lib/postgresql/data
              # copy the sql script to create tables
              #- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
              #- ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh
              - ./sql:/docker-entrypoint-initdb.d #put all .SQL files inside sql/ folder and all files will be executed.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search