Skip to content

Instantly share code, notes, and snippets.

@DTailor
Last active September 15, 2019 18:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save DTailor/5948970 to your computer and use it in GitHub Desktop.
Save DTailor/5948970 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 prostgres 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]
@MrOlek
Copy link

MrOlek commented Jun 17, 2016

The easiest way to make PostgreSQL backup is to use this tool http://postgresql-backup.com/

@quevon24
Copy link

quevon24 commented Aug 2, 2016

Thanks, it helped me because other commands that i was trying always displayed some errors, this is the simplest way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment