Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przbadu/fc925d4abf92b7fb41ba7c153fec6d7b to your computer and use it in GitHub Desktop.
Save przbadu/fc925d4abf92b7fb41ba7c153fec6d7b to your computer and use it in GitHub Desktop.

pull postgres image

docker pull postgres:latest

Run docker container

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

In pg admin or anywhere else you can use above container with:

host: localhost
port: 5432 (or 5433)
username: postgres
password: postgres

Backup and Restore using docker

# 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

Copy dump file into your volumn directory

Example:

NOTE: postgres is the name of my container, it might be different for you, you can also set name for containers while running docker run --name something

docker cp ~/Downloads/ig.sql postgres:/var/lib/postgresql/data

Restore database

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment