Skip to content

Instantly share code, notes, and snippets.

@valentinvieriu
Last active July 4, 2023 15:32
Show Gist options
  • Save valentinvieriu/b427a77acbd5b170c6e5a8752e7d7a5e to your computer and use it in GitHub Desktop.
Save valentinvieriu/b427a77acbd5b170c6e5a8752e7d7a5e to your computer and use it in GitHub Desktop.
Backup, restore postgres in docker container
  • make sure you mounted /var/lib/postgresql/data/ as a volume
  • to backup the database run this over docker exec replace tools_postgres_1 with the name of your postgres container
docker exec tools_postgres_1 bash -c "pg_dump --username=postgres --no-password --format=t db_name | gzip > /var/lib/postgresql/data/backup/backup_local_$(date +%Y-%m-%d).tar.gz"
  • restore the database using portainer console
docker exec tools_postgres_1 bash -c "pg_restore --username=postgres --no-password --dbname=db_name --verbose /var/lib/postgresql/data/backup/backup_local_29-01-2017.tar.gz"
  • to drop the old database use
docker exec tools_postgres_1 bash -c "dropdb -U postgres db_name"
docker exec tools_postgres_1 bash -c "createdb -U postgres db_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment