docker pull postgres:latest
NOTE: If your local postgres installation is running in port 5432, then bind different port with
5433:5432
docker run --name="postgres" -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -v $HOME/docker/volumes/postgres:/var/lib/postgres/data -p 5433:5432 postgres
host: localhost
port: 5432 (or 5433)
username: postgres
password: postgres
# we need container name and id
docker ps
# inspect volume using container id
docker inspect -f '{{ json .Mounts }}' <container id> | python3 -m json.tool
Then, look at the volume paths under the key Destination
.
You should get the following:
[
{
"Type": "volume",
"Name": "9863fbf259be403ba023ac96326e9965ea2cf4369ad570a5f22023564d20f60e",
"Source": "/var/lib/docker/volumes/9863fbf259be403ba023ac96326e9965ea2cf4369ad570a5f22023564d20f60e/_data",
"Destination": "/var/lib/postgresql/data",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
]
In above example it is: /var/lib/postgresql/data
Example:
NOTE:
postgres
is the name of my container, it might be different for you, you can also set name for containers while runningdocker run --name something
docker cp ~/Downloads/ig.sql postgres:/var/lib/postgresql/data
docker exec <container name> pg_restore -U <user> -d <db_name> <backup path>
example
docker exec postgres pg_restore -U postgres -d demo /var/lib/postgresql/data/ig.sql