Postgresql restore database from backup

Restoring the PostgreSQL database from a backup can be done using the command line or a graphical interface. In this article, I will show you how it can be done through the command line.

Step 1: Creating a new database
Before starting the recovery process from the backup, you need to create a new database to save information. Run the following command:

“`
createdb -U <имя_пользователя> -h <хост> - p <порт> <название_новой_базы>
“`

Here `<имя_пользователя>` represents your PostgreSQL user name, `<хост>` and `<порт>` is the host and port of the database server, respectively (usually localhost and 5432), and `<название_новой_базы>` is the name of your new database.

Step 2: Recovery from backup
First, make sure that the backup file is in the right place, accessible for reading.
Run the following command:

“`
pg_restore –dbname=<название_new_db> –username=<имя_user_backup_file_owner>–verbose “
“`

Where:
– `–dbname` indicates the name of the database that was just created;
– `–username` indicates the name of the user who was the owner of the backup copy;
– `–verbose` allows you to display detailed information about the recovery process;
– `` is the full path and name of the backup file.

After completing this command, the database will be successfully restored from your backup.

View all data recovery questions