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
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
You can run a sql command file against a running postgres container via the one liner
cat ./query.sql | docker exec -i <container-name> psql -U <user> -d <database>
without having to copy the file from your host machine to the container.
How to run sql script in sequence using docker compose
How to runnig sql script file
run sql script in postgresql docker have included in DockerFile - Stack Overflow
How to apply SQL query on docker
Videos
That's a feature of the standard postgres image; it's not something you get if you install PostgreSQL by hand in a plain Ubuntu container.
If you build a custom image for this, you can just start from that image and add your initialization file. This is a complete Dockerfile:
FROM postgres:9.6
COPY ./scripts/test.sql /docker-entrypoint-initdb.d/
I might not build a custom image for this. Instead, you can use the docker run -v option or the Docker Compose volumes: option to push the script into the container when it starts up (treating it as a configuration file).
(Also remember that these init scripts only run the very first time the database container starts up; if there is already database data they will not re-run.)
Clean the files/folders inside /var/lib/postgresql/data volume and create a container.
docker exec -it yiialkalmi_postgres_1 psql -U project -W project
Some explanation
docker exec -itThe command to run a command to a running container. Theitflags open an interactive tty. Basically it will cause to attach to the terminal. If you wanted to open the bash terminal you can do this
docker exec -it yiialkalmi_postgres_1 bash
yiialkalmi_postgres_1The container name (you could use the container id instead, which in your case would be40e39bd0329a)psql -U project -W projectThe command to execute to the running containerUuserWTell psql that the user needs to be prompted for the password at connection time. This parameter is optional. Without this parameter, there is an extra connection attempt which will usually find out that a password is needed, see the PostgreSQL docs.projectthe database you want to connect to. There is no need for the-dparameter to mark it as the dbname when it is the first non-option argument, see the docs:-d"is equivalent to specifying dbname as the first non-option argument on the command line."
These are specified by you here
environment:
POSTGRES_DB: project
POSTGRES_USER: project
POSTGRES_PASSWORD: project
This worked for me:
goto bash :
docker exec -it <container-name> bash
from bash :
psql -U <dataBaseUserName> <dataBaseName>
or just this one-liner :
docker exec -it <container-name> psql -U <dataBaseUserName> <dataBaseName>
helps ?
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 ?