The Docker way would be to simply spin up a second container with the dev database. services: prod: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_prod:/postgres/var dev: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_dev:/postgres/var volumes: pg_prod: pg_dev: Answer from ElevenNotes on reddit.com
🌐
Docker Community
forums.docker.com › docker engine › compose
How to run sql script in sequence using docker compose - Compose - Docker Community Forums
December 21, 2022 - Many thanks, Sequence of steps: Download image & create postgres instance Run the flyway job Execute “amend_user.sql” in postgres database In the below docker-compose.yaml, this script runs as soon as the service starts whereas i would like ...
🌐
Medium
medium.com › @aedemirsen › execute-sql-commands-at-postgresql-db-startup-with-docker-2be0abadec48
Execute Sql Commands At Postgresql DB Startup With Docker | by Ahmet Emre DEMİRŞEN | Medium
January 17, 2026 - version: "3.8" services: postgresql: image: postgres container_name: postgres_db restart: always environment: POSTGRES_DB: ${POSTGRES_DB} POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} POSTGRES_ROOT_PASSWORD: ${POSTGRES_ROOT_PASSWORD} ports: - ${POSTGRES_PORT}:5432 volumes: - ./postgres/data:/var/lib/postgresql/data - ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql · Above we have prepared a docker-compose file configured with basic settings.
Discussions

How to run a script PostgreSQL with Docker Compose in the initialization ?
The Docker way would be to simply spin up a second container with the dev database. services: prod: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_prod:/postgres/var dev: image: 11notes/postgres:16 restart: always environment: POSTGRES_PASSWORD: *********** volumes: - pg_dev:/postgres/var volumes: pg_prod: pg_dev: More on reddit.com
🌐 r/docker
6
1
June 20, 2024
psql - How do I run a sql file of inserts through docker run? - Stack Overflow
Given a file with a SQL insert: INSERT INTO countries (id, country_code, name) VALUES (1, 'AF', 'Afghanistan'), (2, 'AL', 'Albania'); I would like to run the file by using the docker run c... More on stackoverflow.com
🌐 stackoverflow.com
postgresql - How do I run SQL Scripts after DB Initialized from Docker-Compose? - Stack Overflow
I have the Docker Compose file below. I'm trying to run the following: Set up Postgres Run Entity Framework to set up my schemas/tables Set up PG Admin Run some SQL scripts on the database. The I... More on stackoverflow.com
🌐 stackoverflow.com
postgresql - Run script after container entrypoint in docker-compose - Stack Overflow
You can put your database ... /docker-entrypoint-initdb.d. This executes all *.sh and *.sql files in this directory and does not touch the original command. All files in this directory are automatically executed in the alphabetical order on container creation. Therefore, create a volume to add your scripts / sql files to the entrypoint and let the container execute them. This is described in the official postgres documentation, section "How to extend this image". Your compose file then ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › onjin › 2dd3cc52ef79069de1faa2dfd456c945
example docker compose for postgresql with db init script · GitHub
If i remove this directory before starting docker-compose file things work !!. I that directory/volume is present the init scripts are ignored !! enjoy!! ... init scripts are run when there postgres-data folder is created.
🌐
Docker
docs.docker.com › guides › pre-seeding database with schema and data at startup for development environment
Pre-seeding database with schema and data at startup for development environment | Docker Docs
$ cat seed.sql | docker exec -i postgres psql -h localhost -U postgres -f- Once the query is executed, you will see the following results: CREATE DATABASE You are now connected to database "sampledb" as user "postgres".
🌐
Reddit
reddit.com › r/docker › how to run a script postgresql with docker compose in the initialization ?
r/docker on Reddit: How to run a script PostgreSQL with Docker Compose in the initialization ?
June 20, 2024 -

I have this:

services:
  db:
    image: postgres:16.3-alpine3.20
    restart: always
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 
      POSTGRES_DB: pento
    volumes:
      - ./init-db.sql:/docker-entrypoint-initdb.d/init-db.sql
      - pg_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    
volumes:
  pg_data:

And I would like to CREATE other DATABASE when container is starting using a sql script.

For example:

/init-db.sql
CREATE DATABASE pento_dev;

How can I do that ? Is it possible ?

🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
$ docker run --name some-postgres -e POSTGRES_PASSWORD_FILE=/run/secrets/postgres-passwd -d postgres Copy · Currently, this is only supported for POSTGRES_INITDB_ARGS, POSTGRES_PASSWORD, POSTGRES_USER, and POSTGRES_DB. If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary).
Find elsewhere
🌐
DEV Community
dev.to › saiful7778 › setting-up-postgresql-with-docker-compose-for-development-and-production-45j8
Setting Up PostgreSQL with Docker Compose for Development and Production - DEV Community
November 29, 2025 - Working with PostgreSQL becomes much easier when you run it inside Docker. It gives you a clean environment, consistent versions, and a quick setup for both development and production. In this guide, we will walk step by step through a practical setup using Docker Compose. ... Let us begin by reviewing the folder layout. ... infra/ ├─ docker/ │ ├─ docker-compose-dev.yml │ └─ docker-compose.yml ├─ postgres/ │ ├─ init-scripts/ │ │ └─ init.sql ...
🌐
Cybertec-postgresql
til.cybertec-postgresql.com › posts › docker-compose for postgres with db init script
docker-compose for postgres with db init script
November 6, 2019 - postgres: image: postgres volumes: - ./init.sql:/docker-entrypoint-initdb.d/init.sql · Multiple scripts run in alphabetical order, thus it's good practice to prefix them with a number. volumes: - ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql - ./data.sql:/docker-entrypoint-initdb.d/2-data.sql ·
🌐
Docker Community
forums.docker.com › general
How to copy script to container inside a docker-compose.yml - General - Docker Community Forums
January 9, 2022 - Hello All , My working environment is windows10, wsl2, ubuntu 20.04 LTS Docker Desktop set for wsl2 I wanted to run sql scripts right from a docker-compose.yml in order to populate a new postgres DB.None of the examples I found worked . Finally I solved my issue following an excellent post ...
Top answer
1 of 3
78

It didn't work for me with the COPY approach in Dockerfile. But I managed to run my init.sql file by adding the following to docker-compose.yml:

    volumes:
        - ./init.sql:/docker-entrypoint-initdb.d/init.sql

init.sql was in the same directory as my docker-compose.yml. I picked the solution from this gist. Check this article for more information.

2 of 3
62

I dont want to have to enter psql in order to type in

You can simply use container's built-in init mechanism:

COPY init.sql /docker-entrypoint-initdb.d/10-init.sql

This makes sure that your sql is executed after DB server is properly booted up.

Take a look at their entrypoint script. It does some preparations to start psql correctly and looks into /docker-entrypoint-initdb.d/ directory for files ending in .sh, .sql and .sql.gz.

10- in filename is because files are processed in ASCII order. You can name your other init files like 20-create-tables.sql and 30-seed-tables.sql.gz for example and be sure that they are processed in order you need.

Also note that invoking command does not specify the database. Keep that in mind if you are, say, migrating to docker-compose and your existing .sql files don't specify DB either.

Your files will be processed at container's first start instead of build stage though. Since Docker Compose stops images and then resumes them, there's almost no difference, but if it's crucial for you to init the DB at build stage I suggest still using built-in init method by calling /docker-entrypoint.sh from your dockerfile and then cleaning up at /docker-entrypoint-initdb.d/ directory.

🌐
ASUS
koskila.net › how-to-run-sql-commands-in-a-postgre-sql-docker-container
How to run SQL commands in a Postgre SQL Docker container? - Koskila.net
(I know, I know, that's technically a TimescaleDB, but it's built on Postgre, so..) ... Here's where to do that in Docker Desktop, but you could do the same by having to run your container in interactive mode. This image has an empty alt attribute; its file name is image-9.png · Or using a console of your choice, it'll look somewhat like this: docker exec -it TimescaleDB bash
🌐
Stack Overflow
stackoverflow.com › questions › 72994749 › how-can-i-make-an-init-sql-script-run-when-using-docker-with-postgresql
How can I make an init.sql script run when using Docker with Postgresql? - Stack Overflow
5 run sql script in postgresql docker have included in DockerFile · 13 Initialize PostgreSQL Container with docker-entrypoint-initdb.d script · 4 Docker compose Postgresql runs init sql scripts only the first time
Top answer
1 of 3
3

Do you have two Dockerfiles? Looks like you built your own MySQL container?

Otherwise, these shouldn't be part of your Java multi-stage build

FROM mysql:5.7

ADD ./database/tshirtshop.sql /docker-entrypoint-initdb.d

Assuming that you did build a separate image for mysql, in the Docker-Compose, you're not using it, as you're still referring to image: mysql:5.7

Rather than building your own, you should mount the SQL script into it

For example

  mysqldb:
    image: mysql:5.7
    ...
    volumes:
      - db-data:/var/lib/mysql
      - ./database/tshirtshop.sql:/docker-entrypoint-initdb.d/0_init.sql

Then, forget the Java service for a minute and use MySQL workbench or the mysql CLI to verify that data is actually there. Once you do, then startup the API

2 of 3
2

If you copying sql scipt already to docker build then you do not need to mapped it again in the docker-compose, if you have docker-compose then you do not the bash script single command docker-compose up --build will do the job.

So modify your docker-compose as per your Dockerfile.

Dockerfile

FROM mysql

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

docker-compose

version: '3.7'

services:
  # App backend service
  app-server:
    # Configuration for building the docker image for the backend service
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080" # Forward the exposed port 8080 on the container to port 8080 on the host machine
    restart: always
    depends_on: 
      - mysqldb # This service depends on mysql. Start that first.
    environment: # Pass environment variables to the service
      SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/tshirtshop?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root   
    networks: # Networks to join (Services on the same network can communicate with each other using container name)
      - uringmysql

      # Database Service (Mysql)

  mysql:
    build: .
    environment:
      MYSQL_ROOT_PASSWORD: root123
      MYSQL_DATABASE: appdata
      MYSQL_USER: test
      MYSQL_PASSWORD: root123
    volumes:
      - db-data:/var/lib/mysql
    tty: true
# Volumes
volumes:
  db-data:

# Networks to be created to facilitate communication between containers
networks:
  turingmysql:

Now just run

docker-compose up --build

this will build and up the container and you will not need to mapped the host init script, as it already in Docker image.

The directory structure will look like

Now you application will able to access DB using jdbc:mysql://mysqldb:3306/tshirtshop? this endpoint as both are in same network and can refer eacher other using name.

🌐
Mkyong
mkyong.com › home › docker › how to run an init script for docker postgres
How to run an init script for Docker Postgres - Mkyong.com
September 22, 2023 - Access the running container and display the data inserted by init.sql. ... docker exec -it pg-container-name psql -U mkyong -d mydb psql (15.4 (Debian 15.4-1.pgdg120+1)) Type "help" for help. mydb=# select * from students; id | name | age ----+--------+----- 1 | Mkyong | 40 2 | Ali | 28 3 | Teoh | 18 (3 rows) mydb=# Exit the terminal. ... P.S. If the PostgreSQL container fails to start, we can use docker logs container-name to check the errors.