Open In App

PostgreSQL – Restore Database

Last Updated : 13 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Restoring a PostgreSQL database is an essential process for data recovery, especially when dealing with data loss, corruption, or unintentional errors. The ability to efficiently restore a database ensures that an organization can maintain data integrity, availability, and consistency.

In this article, we will guide us through the process of restoring PostgreSQL databases, covering both the PgAdmin GUI method and the command-line approach.

Why Restoring a PostgreSQL Database is Important

Restoring a database is important for maintaining business continuity. In cases of data corruption, server crashes, user errors, or even malicious attacks, we can use backup data to restore the database to its previous state. PostgreSQL provides powerful tools like pg_restore and psql that enable us to recover data efficiently, even for large-scale databases

Methods for Restoring a PostgreSQL Database

In PostgreSQL, we can restore a database using two primary methods. using the PgAdmin GUI and via the command line with pg_restore. Both methods are effective and can be chosen based on your preference. Here we are going to see both approaches in detail.

1. Restoring a Database Using PgAdmin GUI

PgAdmin is a popular PostgreSQL administration tool that provides a graphical user interface (GUI) for managing databases. It simplifies the process of restoring a database, especially for users who prefer not to work with the command line. Restoring a database via PgAdmin4 is a user-friendly and straightforward process.

Step-by-Step Guide to Restore via PgAdmin

Step 1: Start PgAdmin4 and Log In

Launch the pgAdmin database server and log in using valid credentials.

Step 2: Navigate to Databases

Expand the ‘Servers’ section and navigate till ‘Databases’. Now, here you will find all your databases the server is currently hosting (if any).

Srtep 3: Create a Placeholder Database

Now we need to create a new database which will serve as a placeholder for our database which is to be restored. For this,

  • Right click on the “Databases” section, select Create -> Database.
  • In the “General” tab, specify the name of the new database and ensure the owner is the current postgres Admin username. Optionally, add comments.
  • In the “Definition” tab, select appropriate character encoding, tablespaces, etc.
  • Fill in the required details in tabs like “Security” and “Parameters.”
  • Click “Save” to create the database, which will appear in the “Databases” section.

Create a Placeholder Database

Step 4: Initiate Restore Process

  • Right-click on the newly created database by you and select “Restore” option from the list.

  • A pop-up dialogue box will appear. Specify the format of the file and browse for its location using the horizontal kebab menu. Select the role name as postgres (root owner).
  • Click the “Restore” button.

Initiate Restore Process

Step 5: Monitor Restore Job

  • A new restore job is now created on the pgAdmin4 server and usually appears in the bottom right corner of the window.
  • After successful restoration, a confirmation message will be displayed. If there’s an error, PgAdmin will show a failure message along with further details, when we click on the More Details (i) button.

Monitor Restore Job

Step 6: Completion

After the restore operation is complete, we can verify the database and its data. Your PostgreSQL database is now fully restored and ready to use.

2. Restoring a Database Using the Command Line

PostgreSQL also allows us to restore a database via the command line using the pg_restore utility. This method is particularly useful for automated backups or advanced users who prefer working with the terminal. To restore a database via the command line, we are going to follow the following procedure:

Step-by-Step Guide to Restore Using Command Line

Step 1: Login to PostgreSQL Terminal

Firstly, we need to login to the PostgreSQL terminal via command line. To do so, use the following PostgreSQL command:

psql -U <username>

Login to PostgreSQL Terminal

Step 2: Create a Placeholder Database

  • We can now see that we are successfully logged into the psql client terminal and have got the postgres command line input prompt.
  • We will now follow the same process as we followed in the above part I, the only difference being here is that, we are going to do this via the command line terminal of PostgreSQL.

Let’s now create a placeholder database for our purpose which will be used to restore the backup. To do so, run the below script.

CREATE DATABASE BackupDB ENCODING='UTF-8' OWNER='postgres';

Create a Placeholder Database

Now, Database is created. Let us restore it for further process.

Step 3: Exit the psql Terminal:

To restore the database, we are going to use the ‘pg_restore’ command supplied with some arguments. It is important to note here that, we need to exit from the psql terminal in order to run ‘pg_restore’ command. To exit from psql terminal,

\q

Step 4: Run pg_restore Command:

Key in the ‘pg_restore’ command with the following arguments:

pg_restore -U postgres -d backupdb -v "D:\Backup.sql"

Arguments Explanation:

  • '-U': Specifies the username.
  • '-d': Specifies the database to restore.
  • '-v': Enables verbose mode to show detailed restoration process.

Step 5: Verify Restoration

After successful restoration of database, we will se that our schema are restored alongwith the tables and their data.

Verify Restoration

Conclusion

Restoring a PostgreSQL database is an important skill for database administrators. Whether we’re using PgAdmin GUI for a more user-friendly approach or pg_restore from the command line for more flexibility, knowing how to restore our database quickly can save our business from potential downtime and data loss. Regular database backups and a tested restoration strategy are key to maintaining data integrity and ensuring that your PostgreSQL database remains highly available even in the face of disaster



Next Article

Similar Reads