How to Restore Data Dump Using pg_restore
Step 1: Find the name and id of the Docker container hosting the Postgres instance
Turn on your Docker and run the docker ps command to locate the name and id of the Docker container. Which leads to the following.
$ docker ps
CONTAINER ID ... NAMES
abc985ddffcf ... my_postgres_1
Step 2: Find the volumes available in the Docker container
Run the command docker inspect -f '{{ json .Mounts }}' <container_id> | python -m json.tool
Then, look at the volume paths under the key Destination.
You should get the following:
$ docker inspect -f '{{ json .Mounts }}' abc985ddffcf | python -m json.tool
[
{
"Type": "volume",
"Name": "my_postgres_backup_local",
"Source": "/var/lib/docker/volumes/my_postgres_backup_local/_data",
"Destination": "/backups",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
},
{
"Type": "volume",
"Name": "my_postgres_data_local",
"Source": "/var/lib/docker/volumes/my_postgres_data_local/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
}
]
In this case, we have /backups and /var/lib/postgresql/data as the volume paths.
Step 3: Copy dump into one of the volumes
Pick a volume and copy your dump in. Run docker cp </path/to/dump/in/host> <container_name>:<path_to_volume>
In my case, I pick the volume /backups. Which gives us the following.
$ docker cp my_data.dump my_postgres_1:/backups
Step 4: Get the database owner to run pg_restore command
Execute the pg_restore command via the docker exec command. Which means the generic forms of both commands are the following.
Note: I'm assuming that the destination postgres database already exists.
For pg_restore:
pg_restore -U <database_owner> -d <database_name> <path_to_dump>
For docker exec:
docker exec <container_name> <some_command>