Skip to content

Instantly share code, notes, and snippets.

@devhero
Last active April 29, 2022 10:16
Show Gist options
  • Save devhero/b4dc765b7760d582369e to your computer and use it in GitHub Desktop.
Save devhero/b4dc765b7760d582369e to your computer and use it in GitHub Desktop.
postgres backup/restore
sudo su postgres
# BACKUP
vacuumdb -U postgres --full --analyze --dbname $DBNAME && pg_dump $DBNAME -Ft -v -U postgres -f /tmp/$DBNAME.tar
# RENAME TARGET DB
psql
alter database $T_DBNAME rename to NEWNAME
# OR DROP TARGET DB
dropdb $T_DBNAME
# RESTORE ON TARGET DB
createdb --encoding UNICODE $T_DBNAME && pg_restore /tmp/$DBNAME.tar | psql --dbname $T_DBNAME && vacuumdb --full --analyze --dbname $T_DBNAME
# RESTORE FROM SQL
psql dbname -f dump.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment