How to Backup and Restore PostgreSQL Database [A Comprehensive Guide]

Backup and restore PostgreSQL database can help users and business to protect data security, and make easy to restore when facing disaster. So, it is essential to configure backup and restore PostgreSQL database.

Amelia

By Amelia / Updated on March 7, 2025

Share this: instagram reddit

Ensuring the security and availability of PostgreSQL databases is critical for organizations, as critical data loss can lead to downtime and financial loss. In this article, you will be introduced to the importance of backing up and restoring PostgreSQL and how to backup and restore PostgreSQL database.

how to backup and restore postgresql database

Why Backup and Restore PostgreSQL Database

PostgreSQL is a powerful open-source relational data management system, but it is as prone to data loss as any database due to hardware failures, software crashes, and other errors. So, regular backups are crucial for:

  • Preventing data loss.
  • Ensure business continuity.
  • Simplifies database migration and recovery.

How to Backup and Restore PostgreSQL Database

The following section provides you with several ways to backup and restore PostgreSQL.

How to Backup a PostgreSQL Database

🔶Method 1. Using pg_dump to Backup
pg_dump allows you to create a single database backup that can generate a SQL script that can be used to recreate the database.

1. Open the Command Prompt: Press Win + R, type cmd, and press Enter.

2. Use the cd command to navigate to the PostgreSQL bin directory. For example:

cd C: \ Program
Files \ PostgreSQL \ 13 \ bin

3. Execute the following command to create a backup:

pg_dump -U username -d database_name -f backup_file.sql

Remember to replace username with your PostgreSQL username, dbname with the name of the database you want to backup, and backup_file.sql with the desired output file name.

4. When prompted, enter the password for the PostgreSQL user.

🔶Method 2. Using pg_dumpall to Backup

pg_dumpall backs up all databases in a PostgreSQL cluster, including roles and tablespaces.

Execute the following command:

pg_dumpall -U username -d database_name -f backup_file.sql

How to Backup Automatically PostgreSQL Database in Windows

Automating the backup process ensures that your data is continuously backed up without human intervention. On Windows, you can use a task scheduler to automate PostgreSQL database backups.

1. Create a batch file that contains the pg_dump or pg_dumpall command. For example, create a file named backup.bat with the following content:

@echo off
cd C: \ Program
Files \ PostgreSQL \ 13 \ bin
pg_dump -U username -d dbname -f
C: \ backups \backup_%date: ~date: ~10, 4%-
%date: ~4, 2%-%date: ~7, 2%. sql

2. Press Win + R, type tasksch, msc, and press Enter.

3. In Task Scheduler, click on Create Basic Task and follow the wizard to set up a new task.

4. Choose how often you want the backup to run (e.g. daily, weekly).

5. Select Start a program, and browse to batch file you created earlier.

6. Complete the wizard and save the task.

How to Restore a PostgreSQL Database

Method 1. Using psql to Restore

psql allows you to execute SQL commands and scripts that you can use to restore a database from a backup file created by pg_dump or pg_dumpall.

1. Press Win + R, type cmd, and press Enter.

2. Use the cd command to navigate to the PostgreSQL bin directory.

3. Execute the following psql command:

psql -U username -d new_database_name -f backup_file.sql

4. When prompted, enter the password for the PostgreSQL user.

Method 2. Using pg_restore to Restore

pg_restore allows you to restore a PostgreSQL database from a backup file created by pg_dump in a custom or directory format.

Execute the following pg_restore command:

pg_restore -U username -d new_database_name -1 backup_file.sql

Additional: An Easy and Efficient Way to Backup Microsoft SQL

AOMEI Cyber Backup is a professional backup software designed for Microsoft SQL database security, providing you with a variety of measures to ensure the security of the SQL database.

Download FreewareMicrosoft SQL Server 2005-2022
Centralized and Secure SQL Backup

Conclusion

Backup and restore PostgreSQL database is a critical task that ensures data integrity and business continuity. By using tools like pg_dump, pg_dumpall, psql and pg_restore, you can easily and restore backups. Additionally, automating the backup process in Windows using Task Scheduler can save time and reduce the risk of human error.

Amelia
Amelia · Editor
Amelia is an editor from AOMEI. Trained in professional systems, she specializes in solving problems in enterprise databases, virtual machines, and physical environments. With a high sense of responsibility, she is dedicated to protecting business data and security.