Sonu Kumar
2 min readFeb 20, 2024

Effortless Database Migration: Dumping from Remote to Local and Dockerizing environment

How to Dump a Remote Database to Your Local Machine and Import it into a Dockerized Environmen

If you’re running PostgreSQL in a Docker container and want to import the dump database into that container, you need to follow a slightly different approach. Here’s what you can do:

Dumping the Remote Database:

  1. Open a terminal or command prompt on your local machine.
  2. Use the pg_dump command with the appropriate options to dump the remote PostgreSQL database. For example:
pg_dump -h <remote_host> -U <username> -d <database_name> -Fc -f <dump_file_name>

Import it into a Dockerized Environment:

  1. Find the Container ID or Name: First, find the ID or name of your PostgreSQL container. You can use the docker ps -a command to list all running containers.

2. Copy the Dump File into the Container: Use the docker cp command to copy the dump file from your local machine into the PostgreSQL container. For example:

docker cp /path/to/dev-database.dump <container_id_or_name>:/tmp/dev-database.dump

3. Restore the Dump Inside the Container: Once the dump file is copied into the container, you can execute the pg_restore command inside the container to restore the database. You can use the docker exec command to run a command inside the container. For example:

docker exec -it <container_id_or_name> pg_restore -U <username> -d <database_name> -Fc /tmp/dev-database.dump

Replace <username> your PostgreSQL username, <database_name> with the name of the database you want to import into, and <container_id_or_name> with the ID or name of your PostgreSQL container.

For instance:

docker exec -it my-postgres-container pg_restore -U postgres -d mydatabase -Fc /tmp/dev-database.dump

4. Enter the Password if Required: If your PostgreSQL server inside the container requires a password, it will prompt you to enter it after executing the pg_restore command.

Sonu Kumar

Full stack Web developer with experience and I love reading article such blogs books and writing.