How to Dump and Restore PostgreSQL Database?
Last Updated :
17 Oct, 2024
PostgreSQL remains among the most efficient and widely applied open-source relational database management systems. It provides the superior function of saving, configuring, and extracting information most effectively. In the process of migrating data, creating backups, or transferring databases between environments, knowing how to dump and restore PostgreSQL is essential.
In this article, we will go through the individual steps to dump and restore PostgreSQL databases using both the command-line interface (CLI) and pgAdmin, a popular graphical user interface for PostgreSQL management.
How to Dump a PostgreSQL Database?
A PostgreSQL dump file is a text file consisting of SQL statements that can recreate a database’s structure and content. This "backup" file can be used later to restore the database. Here, we will discuss two methods for dumping a PostgreSQL database: using the pg_dump command-line tool and pgAdmin.
Method 1: Using the pg_dump Command-Line Tool
The pg_dump command-line utility is the most commonly used method to dump a PostgreSQL database. It creates a logical backup, saving the database as a plain-text file or in a custom format.
Step 1: Open Terminal or Command Prompt
Start by opening the Terminal on Linux/Mac or Command Prompt on Windows.
Step 2. Navigate to PostgreSQL Bin Directory (Optional)
If the path to PostgreSQL's bin directory is not added to your system's PATH, a command 'cd' will be used to navigate to the directory. This directory is usually located within your PostgreSQL installation directory.
Step 3. Execute the pg_dump Command
The command for the pg_dump will be "pg_dump database_name". Optional parameters including username, hostname, and port settings can be specified here.
pg_dump -U username -h hostname -p port dbname > dump.sql
- Replace username with your PostgreSQL username.
- Replace hostname with the server where the database is located (use "localhost" if local).
- Replace port with the port number (default is 5432).
- Replace dbname with the name of the database we want to dump.
- Replace dump.sql with the name of your dump file.
Step 4. Enter Password (If Required)
Enter the password associated with the corresponding username when prompted.
Step 5. Verify Dump
After all the commands finish running, check that a file named 'dump.sql' (or any other name we want) has been created inside this directory. It will be in the current directory. The data file saves the SQL statements in order to set up the database structure and to insert the data.
Example:
pg_dump -U postgres -h localhost -p 5432 -d northwind > C:\Users\Sanket\Desktop\northwind_backup.sql
Output
Using the pg_dumpExplanation:
Once we have completed these steps, we dump the Northwind database via the use of 'pg_dump' command line tool. Now the dump file is ready for restoring the database later or moving the database to another PostgreSQL instance.
Database administrators as well as developers perform dumping databases as a regular one-off operation to preserve the database data integrity and uptime in different scenarios.
Method 2: Using pgAdmin
For users who prefer a graphical interface, pgAdmin provides an easy way to create database backups.
Step 1: Open pgAdmin
Launch pgAdmin connect to your PostgreSQL server.
Step 2: Navigate to the Databases Section
In the object browser, click the + button to widen the server node, the Databases section under it.
Step 3: Right-click on the Database
Right-click on the database you want to dump and select "Backup."
BackupStep 4: Specify Backup Options
A dialog box will appear, allowing you to select the backup type (plain, custom, tar) and specify the filename.
Specify backup optionsStep 5: Initiate Backup
Clicking on "Backup" to initiate the backup process. At the finishing point, you are left dump file that holds the database schema and data in it.
Initiate BackupHow to Restore a PostgreSQL Database?
PostgreSQL database recovery is one of the most important tasks in database administration. It can be used to recover from a backup, migrate data, or create a new environment. In this section, we will go over how to restore a PostgreSQL database. We will use the PostgreSQL command-line tool (psql) as well as the popular graphical interface (pgAdmin) to do this.
Method 1: Restoring Backup Using psql Command-Line Tool
The psql tool is commonly used to restore PostgreSQL backups from a SQL dump file.
Step 1: Open Terminal or Command Prompt
Open your terminal or command prompt window.
Step 2: Connect to PostgreSQL Server
Execute the following command to connect to the PostgreSQL server using the psql command-line tool:
psql -U postgres
Enter the password for the Postgres user if prompted.
Step 3: Create a New Database
Inside the psql environment, create a new database named 'recoverdb':
Step 4: Exit psql Environment
Type '\q
'
to exit the psql environment.
Step 5: Restore Backup
Outside the psql environment, use the following command to restore the backup file to the 'recoverdb' database:
psql -U postgres -d recoverdb -f "C:\Users\Sanket\Desktop\northwind_backup.sql"
Example:
Type the command to recover the dump file in recoverdb database which we created before.
Recovery the dump file Enter password and the database will be recovered from northwind_backup.sql dump file
Recovered data Method 2: Restoring Backup Using pgAdmin
Restoring a database through pgAdmin is easy and user-friendly.
Step 1: Launch pgAdmin
Open pgAdmin and connect to your PostgreSQL server.
Step 2: Create a New Database
Navigate to the Databases section, right-click on the server, and select "Create" > "Database...". Enter recoverdb as the database name and click "Save".
Step 3: Restore Backup
Right-click on the 'recoverdb'
database you just created and select "Restore." In the dialog box that appears, navigate to the location of your backup file ('northwind_backup.sql'
) and select it.
Restore BackupClick "Restore" to initiate the restoration process.
Process startedConclusion
Backup/Restore and database dumping are common tasks for database administrators and developers. Whether you are migrating data, making backups, or transferring databases between environments, understanding how to dump and restore PostgreSQL databases is crucial.
By following the steps outlined in this article, we can reliably and efficiently dump and restore our PostgreSQL databases without risking data integrity or availability. Both pg_dump and pgAdmin offer powerful options for ensuring our database backups are properly created and restored.
Similar Reads
SQL Interview Questions
Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970s, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Tutorial
SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS
In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
8 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
ACID Properties in DBMS
In the world of Database Management Systems (DBMS), transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliabilit
8 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read