Post

Backup and Restore PostgreSQL Database

Backup Database

pg_dump is the PostgreSQL utility to backup the database.
To backup a single database, run below command in command line interface as superuser.
1
~ sudo pg_dump -U postgres -h localhost name-of-database > name-of-backup-file

Restore Database

Backup file created can be useful to restore your system.
To restore it is essential to create a empty database and then you can restore database using following command:
1
psql new_Database_name < path_to_backup_file
Here is the full script to restore the single database
1
2
3
4
sudo su - postgres
postgres@usr-Aspire-E5-575G:~$ psql
postgres=# CREATE DATABASE new_database_name TEMPLATE template0;
postgres@usr-Aspire-E5-575G:~$ psql new_database_name < /home/siv/name-of-backup-file

This post is licensed under CC BY 4.0 by the author.