#Snippets

March 10, 2020

#How to dump and restore PostgreSQL databases

# Binary mode
pg_dump -Fc -U $POSTGRES_USER $POSTGRES_DB > dump-`date '+%Y_%m_%d__%H_%M_%S'`.psqlc
pg_restore -O -U $POSTGRES_USER -c -x -n public -d $POSTGRES_DB dump-2019_03_18__19_03_14.psqlc

# SQL mode
pg_dump -U $POSTGRES_USER -f /backups/dump.sql $POSTGRES_DB
psql -d $POSTGRES_DB -a -f /backups/dump.sql

# Docker and SQL mode
docker exec -it -u postgres <container_name> sh -c 'pg_dump -U $POSTGRES_USER $POSTGRES_DB' > dump-`date '+%Y_%m_%d__%H_%M_%S'`.sql

Section break