Skip to content

Instantly share code, notes, and snippets.

@onjin
Created September 5, 2016 09:17
Show Gist options
  • Save onjin/2dd3cc52ef79069de1faa2dfd456c945 to your computer and use it in GitHub Desktop.
Save onjin/2dd3cc52ef79069de1faa2dfd456c945 to your computer and use it in GitHub Desktop.
example docker compose for postgresql with db init script
postgres:
image: postgres:9.4
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
create table sometable(id int);
@Gui-mp8
Copy link

Gui-mp8 commented Dec 10, 2022

I was facing the same issue of init scripts getting ignored.

I HAVE FOUND A SOLUTION if it helps anyone !!

version: '2'
services:

  common_db:
    image: postgres:10.4
    restart: always
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres123
      - POSTGRES_MULTIPLE_DATABASES=db1,db2
    volumes:
      - ./db.sql:/docker-entrypoint-initdb.d/db.sql
      - ./postgres-data:/var/lib/postgresql/data
    ports:
      - 5436:5432
    networks:
       - app-network

volumes:
  postgres-data:
networks:
  app-network:
    driver: bridge`

Now as you can see I have postgres-data: volume mounted. If i remove this directory before starting docker-compose file things work !!.

I that directory/volume is present the init scripts are ignored !! enjoy!!

GOD!!!

@lumizilla
Copy link

I know its maybe out of topic but asking out of cough cough despair, did anyone find a way to do something like this with an oracle database? "docker-entrypoint-initdb.d" doesn't seem to work for oracle... but worked perfectly when I tried with postgres

@onjin
Copy link
Author

onjin commented Jul 11, 2024

I know its maybe out of topic but asking out of cough cough despair, did anyone find a way to do something like this with an oracle database? "docker-entrypoint-initdb.d" doesn't seem to work for oracle... but worked perfectly when I tried with postgres

It depends on docker image you're using if it is supporting entry scripts and how. For postgres docker image it's you can see execution here:

For your image you need to check the documentation of source code if it's available.

@MartinCastellano
Copy link

postgres:
image: postgres
container_name: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=postgresdb
ports:
- "5432:5432"
volumes:
- ./app/db/init_db/:/docker-entrypoint-initdb.d/
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user -d postgresdb"]
interval: 10s
timeout: 5s
retries: 5

and remember to do chmod 644 of your .sql file ..in my case chmod 644 ./app/db/init_db/init.sql this is muy mucho importante

volumes:
postgres_data:

@janani-reddy9
Copy link

janani-reddy9 commented Jan 28, 2025

I followed this thread and figured out the docker-compose.yml which works. But when I am trying to create a new database or access the table created, or the table's data, I see no output nor any error. It's just new database didn't get created, new table didn't get created. (I tried to connect to the database using docker exec -it containerId bash, then psql -h <hostname> -p <port> -U <username> -d <database>)

Surprisingly, when I do the GET operation from my codebase, it works to retrieve the inserted data. Is this expected or Am I missing something?

@Sharofiddin
Copy link

I'm running into an issue where my db init process is in fact running in the container, at least batching a bunch of CREATES and INSERTS of my dump data, but then is abruptly shut down and restarted.

strends_psql | CREATE DATABASE
strends_psql | You are now connected to database "rivers" as user "usr".
strends_psql | CREATE SCHEMA
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | CREATE TABLE
strends_psql | INSERT 0 4
strends_psql |
strends_psql |
strends_psql | 2019-04-29 17:43:06.633 UTC [41] LOG:  received fast shutdown request
strends_psql | waiting for server to shut down....2019-04-29 17:43:06.637 UTC [41] LOG:  aborting any active transactions

When I get a shell into my db container, the database rivers was created but is empty, with no tables, relations, or data from my batched inserts.

The same issue

@DavidGameDev
Copy link

I'm running into an issue where my db init process is in fact running in the container, at least batching a bunch of CREATES and INSERTS of my dump data, but then is abruptly shut down and restarted.

strends_psql | CREATE DATABASE
strends_psql | You are now connected to database "rivers" as user "usr".
strends_psql | CREATE SCHEMA
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | SET
strends_psql | CREATE TABLE
strends_psql | INSERT 0 4
strends_psql |
strends_psql |
strends_psql | 2019-04-29 17:43:06.633 UTC [41] LOG:  received fast shutdown request
strends_psql | waiting for server to shut down....2019-04-29 17:43:06.637 UTC [41] LOG:  aborting any active transactions

When I get a shell into my db container, the database rivers was created but is empty, with no tables, relations, or data from my batched inserts.

The same issue

Same problem aswell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment