Skip to content

Instantly share code, notes, and snippets.

@calrrox
Forked from DTailor/backup_restore.sh
Last active October 6, 2016 18:32
Show Gist options
  • Save calrrox/8185729412ecf5142242eb19acd4e5d0 to your computer and use it in GitHub Desktop.
Save calrrox/8185729412ecf5142242eb19acd4e5d0 to your computer and use it in GitHub Desktop.
Backup/Restore PostgreSQL Database
# List all databases
sudo -u postgres psql --list
# Create backup file
sudo -u postgres pg_dump [database_name] > dumpl.sql
# Drop the database
sudo -u postgres dropdb [database_name]
# Create a new database
sudo -u postgres createdb [new_databse]
# Restore data from the backup
sudo -u postgres psql [new_database] < dump.sql
#####################################################
# Backup a single table
sudo -u postgres pg_dump --table [table_name] [database_name] > dump.sql
# Restore a single table
sudo -u postgres psql -f dump.sql [database_name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment