Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
How to Setup a PostgreSQL Database Cluster
Next article icon

How to Dump and Restore PostgreSQL Database?

Last Updated : 17 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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_dump
Using the pg_dump

Explanation:

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."

Backup
Backup

Step 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-Options
Specify backup options

Step 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-Backup
Initiate Backup

How 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.

Recover-the-dump-file
Recovery the dump file

Enter password and the database will be recovered from northwind_backup.sql dump file

Recovered-Data
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-backup
Restore Backup

Click "Restore" to initiate the restoration process.

Process-started
Process started

Conclusion

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.


Next Article
How to Setup a PostgreSQL Database Cluster

S

sanketnagare
Improve
Article Tags :
  • PostgreSQL
  • Databases
  • PostgreSQL Query

Similar Reads

  • PostgreSQL - Restore Database
    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 artic
    5 min read
  • How to Setup a PostgreSQL Database Cluster
    A PostgreSQL database cluster refers to a collection of databases managed by a single instance of the PostgreSQL server. Setting up a PostgreSQL cluster is an essential task for organizing multiple databases and achieving high availability, scalability, and load balancing. Whether we are working wit
    5 min read
  • PostgreSQL - Size of a Database
    Efficient database management is essential for ensuring optimal performance in PostgreSQL. One critical aspect of this is monitoring the database size to manage storage and plan for scaling. PostgreSQL offers powerful built-in functions like pg_database_size() to calculate the size of a specific dat
    4 min read
  • How to use PostgreSQL Database in Django?
    This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are
    2 min read
  • How to Migrate a PostgreSQL Database to MySQL
    Moving a database from one platform to another can be tough, but with careful planning and execution, it can be done smoothly. In this article, we'll go over how to migrate a PostgreSQL database to MySQL, which are both popular RDBMS. We'll cover preparation, schema conversion, data migration, and t
    5 min read
  • How to Export PostgreSQL Database Without Data Using SQL?
    When we are working with the PostgreSQL database, there are multiple times we need to export the database structure. This approach is useful when we create a skeleton database or migrate the schema changes for different environments or systems. In this article, we will explore the process of exporti
    3 min read
  • How to set up a PostgreSQL Database with Podman
    Podman is a tool that developers and system administrators are using more and more to manage and deploy their software stacks as the need for containerized applications grows. We will look at how to use Podman to set up and maintain a PostgreSQL database in this tutorial. The overview of PostgreSQL
    7 min read
  • How to Import and Export SQL Server Database?
    Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
    3 min read
  • How to List Databases and Tables in PostgreSQL using PSQL
    PostgreSQL is a powerful, open-source object-relational database system. It provides a wide array of tools and features to manage databases, tables, and other database objects. In this article, we will explain how to list databases and tables in PostgreSQL using the psql command-line interface. We w
    3 min read
  • How to Back Up and Restore a MongoDB Database?
    MongoDB is considered one of the classic examples of NoSQL systems. Its documents are made up of key-value pairs, which are the basic unit of data in MongoDB. Whether we're dealing with accidental data loss, hardware failures, or other unforeseen issues, having a solid backup and restoration plan ca
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences