- Home
- PostgreSQL
- pg_dump: Backup and Restore
pg_dump: Backup and Restore
Many times, we will need to move a postgresql to another server (or just backup the database). To do this, there is an awesome command called pg_dump.
pg_dump is used as follows:
pg_dump dbname -U myusername -F c > /path/to/outfile
Restore it with the following. Replace dbname with the database you are restoring into (make sure this database exists)
pg_restore -C -d dbname -U myusername db.dump
Special Cases
Todays databases are often hosted in the cloud and are connected to by host, port, database, username and password
#Prompts for password pg_dump NameOfDB -U myusername -h host-or-localhost -F c > /the/disk/path/myDB_bak.dump #Oneliner with a password build in (perfect for crontab) PGPASSWORD="myPass123" pg_dump NameOfDB -U myusername -h host-or-localhost -F c > /the/disk/path/myDB_bak.dump