Importing & Exporting PSQL Database Dump using Docker
Source: Dev.to
Prerequisites
- A running PostgreSQL Docker container.
- Your SQL dump file available on the host.
Verify the container is running:
docker ps
You should see your PostgreSQL container listed (e.g., exampledb).
Importing a Dump File into the Container
Copy the dump file from the host into the container:
docker cp path_of_dump_file.sql :/tmp/
Example
docker cp ~/Downloads/example_db.sql exampledb:/tmp/example_db.sql
If successful, Docker prints a confirmation such as:
Successfully copied 23MB to exampledb:/tmp/example_db.sql
The file is now inside the container and can be used to populate the database.
Exporting (Dumping) the Database from Docker
Create a dump of the database and save it on the host machine:
docker exec pg_dump -U > file.sql
Example
docker exec exampledb pg_dump -U root exampledb > example_db_dump.sql
The command generates example_db_dump.sql on your local system, containing a full backup of the database.