There are two commands that can be used to restore from database backups. The psql command will restore plain SQL script files that have been created by pg_dump and pg_dump tools. The pg_restore command is a utility that allows us to restore a PostgreSQL database from an archive that has been created using the pg_dump command using one of the non-plain text formats such as a tar. This command will execute the commands to reconstruct the database to the time in which the database backup was created. Within the archive file, the pg_restore has the ability to identify what items are restored as well as the items to be restored.
First, we will look at the psql tool. By using the psql tool, you can execute the entire SQL script at once. The command will look like the following:
psql -U adminrole -f backupfile.sql
This will log in using the admin role, although you will need to enter in the password and run the backupfile.sql to restore the data. Similar to the pg_dump tool, there are other options that can be passed in:
The pg_restore focuses on restoring databases that are in a non-text format created from the pg_dump or pg_dumpall tools. Using this command, you can specify database objects from a database file that contains full databases or individual databases. This tool can also take a backed-up database from an older version of a database and restore it in a new version.
For example, say we had a backup.tar file that had been created in the same folder. We can restore the database by doing:
pg_restore -d mydb -f backup.tar
We also have options with the pg_restore:
Source: Authored by Vincent Tran