Tutorials  /  Databases

PostgreSQL Backup and Restore with pg_dump & pg_restore

Ccentron Redaktion · July 2026 ·4 min read ·Databases, Tutorial

pg_dump is a PostgreSQL utility that enables administrators to export databases in multiple formats such as full, copy, incremental, and differential backups. Together with pg_dumpall, this tool can export one or several databases in a single command, generating a SQL script like .sql, .dump, or an archive file such as .tar or .tgz. These backups are useful for migrating databases across servers, recovering after failures, or creating snapshots for testing and development.

This guide outlines how to back up PostgreSQL databases using pg_dump and pg_dumpall, and how to restore them with pg_restore on both local and remote PostgreSQL servers.

Prerequisites

Before you proceed, make sure you:

  • Have access to an existing PostgreSQL server.
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Install the PostgreSQL Client

This section shows how to install the PostgreSQL client, which includes command-line tools like pg_dump and pg_restore for backing up and restoring PostgreSQL databases. These utilities allow you to connect to local or remote servers, export data in different formats, and handle migrations or recovery tasks effectively.

Follow these steps for APT-based systems such as Ubuntu and Debian:

Update the APT package index.

Console
$ sudo apt install -y postgresql-client

Install the PostgreSQL client.

Console
$ sudo apt install -y postgresql-client

Run the following command on RPM-based systems like Rocky Linux and AlmaLinux:

Console
$ sudo dnf install -y postgresql

On Windows, visit the PostgreSQL download page and get the latest PostgreSQL client installer. It includes pg_dump, pg_restore, and other command-line utilities.

Back Up a PostgreSQL Database

Use pg_dump and pg_dumpall to back up individual databases or all databases on a PostgreSQL server. General syntax for pg_dump is as follows:

Code
pg_dump --host= --port= --username= --dbname= -f .dump

To back up a specific PostgreSQL database, use the following command and adjust placeholders as needed:

Console
$ pg_dump -Fd -v --host=db.example.com --port=5432 --username=db_user --dbname=example_db -f example_db.dump

Explanation of parameters:

  • -Fd: Outputs in directory format (use -Fc for compressed format).
  • -v: Activates verbose output.
  • -f: Defines the output file or directory.

To back up all databases, run pg_dumpall:

Console
$ pg_dumpall -v --host=db.example.com --port=5432 --username=db_user > full-backup.sql

Note: pg_dumpall only supports plain SQL format. For parallel or custom backups, use pg_dump individually for each database.

After completion, confirm that the backup file exists:

Console
$ ls

Restore a PostgreSQL Database

To restore backups, use pg_restore or psql depending on the backup format. General syntax for pg_restore:

Code
pg_restore --host= --port= --username= --dbname= .dump

Connect to your PostgreSQL server with psql:

Console
$ psql --host=db.example.com --port=5432 --username=db_user --dbname=postgres

Create a target database:

Code
postgres=# CREATE DATABASE example_db;

Note: For single database backups, you need to create the target database manually. For pg_dumpall backups, databases are recreated automatically.

Exit PostgreSQL prompt:

Code
postgres=# \q

Restore a single database with pg_restore:

Console
$ pg_restore --host=db.example.com --port=5432 --username=db_user --dbname=example_db backup.dump

Restore all databases from pg_dumpall backup using psql:

Console
$ psql --host=db.example.com --port=5432 --username=db_user -f full-backup.sql

Explanation:

  • -f: Defines the SQL file to restore.
  • --username: A superuser is generally required to restore roles and permissions.

Note: Make sure the target server is clean and has no conflicting roles or databases before restoring a pg_dumpall backup.

After restoration, connect and switch to the restored database:

Code
postgres=# \c example_db

List tables to confirm restoration:

Code
postgres=# \dt

Verify table data:

Code
postgres=# SELECT * FROM app_users;

Replace app_users with your actual table name.

Exit PostgreSQL client:

Code
postgres=# \q

Conclusion

This guide demonstrated how to back up and restore PostgreSQL databases using pg_dump, pg_dumpall, and pg_restore. These tools provide reliable methods for exporting individual databases or full clusters for migration, testing, and disaster recovery. You installed the PostgreSQL client, created both single and complete backups, and restored data using custom and plain SQL formats. For more advanced strategies, consult the official PostgreSQL Documentation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren