to execute commands against a running container use docker exec.

to copy a file (ex: dump.sql) into a container, use docker cp

So your approach might look something like this:

docker cp ./dump.sql pg_test:/docker-entrypoint-initdb.d/dump.sql
docker exec -u postgres pg_test psql postgres postgres -f docker-entrypoint-initdb.d/dump.sql

here it is in generic form:

docker cp ./localfile.sql containername:/container/path/file.sql
docker exec -u postgresuser containername psql dbname postgresuser -f /container/path/file.sql

And note that if you need to seed your database every time it is run, the folder /docker-entrypoint-initdb.d/ does have special significance, if you're using the offical postgres image

Answer from code_monk on Stack Overflow
🌐
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 - Hello, in this article, I will try to answer this question: “How do we run the SQL commands we want to run on the first startup?” when a postgresql database is being up. Let’s continue through the following headings in order: 1- Postgresql Docker Image and docker-compose · 2- Creating the init.sql File · 3- Let’s Test the Results · Let’s create the following docker-compose.yml file to quickly create the database: 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 ·
Discussions

How to run sql script in sequence using docker compose
I’d like to run a script “amend_user.sql” after the users are created using the flyway job. Please advise. Many thanks, Sequence of steps: Download image & create postgres instance Run the flyway job Execute “amend… More on forums.docker.com
🌐 forums.docker.com
11
0
December 21, 2022
How to runnig sql script file
hello, I want .sql file run with exec command. image: postgres:alpine connection : succesful #docker cp ./test.sql postgresql_database/docker-entrypoint-initdb.d/test.sql # docker exec postgresql_database -u postgres… More on forums.docker.com
🌐 forums.docker.com
4
0
October 16, 2018
run sql script in postgresql docker have included in DockerFile - Stack Overflow
I am learning to use docker, my goal is to create a postgres sql container in which i create a table, as of now i do not want to add any data just create the table. i was able to create a postgres sql instance but copy/add my sql script (which creates this table) to /docker-entrypoint-initdb.d/ ... More on stackoverflow.com
🌐 stackoverflow.com
How to apply SQL query on docker
Please apologize my stupid question - I guess the above are command lines - true? How do they work as docker commands? Cheers, Wolf More on discuss.tryton.org
🌐 discuss.tryton.org
1
0
September 4, 2020
🌐
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
🌐
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
Run the following command on your local terminal: $ docker exec -it postgres psql -h localhost -U postgres · You can now execute any SQL queries or commands you need within the psql prompt.
🌐
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 - I’d like to run a script “amend_user.sql” after the users are created using the flyway job. Please advise. 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 it to run at the end or after step 2. version: '3.8' services: db: container_name: postgresDB image: postgres:15.1 restart: alwa...
🌐
Warp
warp.dev › terminus › docker-postgres-container
How To Launch A PostgreSQL Container In Docker
January 31, 2024 - Copy the local file named script.sql into the /tmp directory of the container named database. Start an interactive shell session by executing the bash command within the database container.
Find elsewhere
🌐
YouTube
youtube.com › watch
How to run sql script for Postgres in Docker? - YouTube
node.js: How to run sql script for Postgres in Docker?Thanks for taking the time to learn more. In this video I'll go through your question, provide various ...
Published   December 28, 2023
🌐
Mat Duggan
matduggan.com › til-the-correct-way-to-load-sql-files-into-a-postgres-docker-container
TIL The correct way to load SQL files into a Postgres and Mongo Docker Container
February 2, 2022 - ... The restore.sh script is below. #!/bin/bash cd /docker-entrypoint-initdb.d || exit ls -ahl mongorestore /docker-entrypoint-initdb.d/ Just do a mongodump into a directory called dump and then when you want it, load the data with the restore.sh script inside the container.
🌐
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 - docker run --name pg-container-name -p 5432:5432 -e POSTGRES_USER=mkyong -e POSTGRES_PASSWORD=password -e POSTGRES_DB=mydb -v /path/to/init.sql:/docker-entrypoint-initdb.d/init.sql -d postgres
🌐
Tryton
discuss.tryton.org › support › system administrator
How to apply SQL query on docker - System Administrator - Tryton Discussion
September 4, 2020 - Please apologize my stupid question - I guess the above are command lines - true? How do they work as docker commands? Cheers, Wolf
🌐
CommandPrompt Inc.
commandprompt.com › education › how-to-useexecute-postgresql-query-in-docker-container
How to Use/Execute PostgreSQL Query in Docker Container — CommandPrompt Inc.
August 14, 2023 - Then, build and run the Postgres container through the “docker run --name -d <cont-name> -p 5432:5432 -e POSTGRES_PASSWORD=<password> postgres” command. Next, interact with the executing container and establish a connection with a database ...
🌐
DZone
dzone.com › software design and architecture › cloud architecture › postgresql with docker – quick start
PostgreSQL With Docker – Quick Start
October 30, 2020 - This was a basic introduction of how to use docker for running PostgreSQL database. We also saw a simple configuration related to environment setup and how to execute scripts as well. Docker file and scripts can be downloaded from this git repository. If you want to know more about Azure Data Studio or SQL...
🌐
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 ?

🌐
Medium
bricefotzo.medium.com › how-to-quickly-run-sql-queries-using-docker-3f310eaf6d3e
How to quickly run SQL queries using Docker?
June 6, 2024 - Having the image on our local Docker registry, let’s run the PostgresSQL server in a container with the command docker run: docker run: This command is used to start a new container from a Docker image. docker run --name sql_container -e ...
🌐
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
To do this, I have such a docker-compose. version: '3' services: postgres: image: postgres:12 restart: always networks: - backend ports: - '5432:5432' volumes: - ./db_data:/var/lib/postgresql/data - ./app/config/init.sql:/docker-entrypoint-initdb.d/create_tables.sql env_file: - ./app/config/.env healthcheck: test: [ "CMD", "pg_isready", "-q", "-d", "devdb", "-U", "postgres" ] timeout: 45s interval: 10s retries: 10 app: build: app ports: - 3200:3200 networks: - backend depends_on: postgres: condition: service_healthy volumes: db_data: networks: backend: driver: bridge
🌐
Docker
hub.docker.com › _ › postgres
postgres - Official Image | Docker Hub
After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service. Warning: scripts in /docker-entrypoint-initdb.d are only run if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.
🌐
Stack Overflow
stackoverflow.com › questions › 42963280 › how-to-run-sql-script-for-postgres-in-docker
node.js - How to run sql script for Postgres in Docker? - Stack Overflow
March 23, 2017 - postgres | CREATE DATABASE postgres | postgres | CREATE ROLE postgres | postgres | postgres | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/00-initial-data.sql postgres | CREATE EXTENSION postgres | CREATE TABLE postgres | postgres | postgres | LOG: received fast shutdown request postgres | LOG: aborting any active transactions postgres | waiting for server to shut down....LOG: autovacuum launcher shutting down postgres | LOG: shutting down postgres | LOG: database system is shut down postgres | done postgres | server stopped postgres | postgres | PostgreSQL init process complete; ready for start up.