Back Up a PostgreSQL Cluster: pg_dump, pg_dumpall and Restore
A logical backup exports the contents of PostgreSQL as a SQL script or archive instead of copying files from the data directory. For a whole cluster you need two tools: pg_dump backs up one database at a time, and pg_dumpall --globals-only backs up roles and tablespaces. This post shows how the two work together, how to restore a single database, and where logical backups end. I checked the pg_dump, pg_dumpall and pg_restore calls on 3 December 2026 against PostgreSQL 18.6; the linked manual describes PostgreSQL 19.
What does pg_dump back up, and what does it not?
pg_dump backs up exactly one database. It writes neither roles nor tablespaces, because those belong to the whole cluster and not to a single database. pg_dumpall covers the whole cluster: it emits commands for roles, tablespaces and empty databases, and then calls pg_dump for every database.
A dump is a snapshot taken when pg_dump starts. The program does not block readers or writers; the exception is operations that need an exclusive lock, such as most forms of ALTER TABLE (as described in the pg_dump manual). So you can back up while the system is running, but you should not schedule schema changes into the same window.
I measured the difference. In the dump of a test database with one owning role I found zero hits for CREATE ROLE but two lines with OWNER TO …. The dump refers to the role, but does not contain it.
How do you back up roles and tablespaces?
With pg_dumpall --globals-only, short -g. The command writes a SQL script that contains only roles and tablespaces, no databases. Run it once per cluster in every backup run, because roles change over time. Without this script, a restore onto a new server fails on owners that do not exist there.
sudo -u postgres pg_dumpall --globals-only -f /data/backup/postgresql/globals.sql
Two things to keep in mind. First, the file contains the password hashes of the roles (one hit for PASSWORD ' in my test); protect it like a secret and create the target directory with mode 0700. With --no-role-passwords, pg_dumpall leaves the passwords out, and you have to set them again after a restore. Second, in its full form pg_dumpall connects once per database and asks for the password every time if you use password authentication. A ~/.pgpass file solves that; details are in the pg_dumpall manual. How to secure roles and privileges in general is practised in the seminar PostgreSQL Security and Hardening (German).
Which format fits: plain, custom or directory?
For single databases the custom format (-Fc) is usually the best choice: one file, compressed by default, and pg_restore can restore single tables or schemas from it. The directory format (-Fd) is the only variant in which pg_dump itself works in parallel (-j). The plain format is a readable SQL script that psql loads.
| Format | Option | Result | Restore single objects | Parallel backup (pg_dump -j) |
Parallel restore (pg_restore -j) |
|---|---|---|---|---|---|
| plain | -Fp (default) |
SQL script | no, only completely with psql |
no | no |
| custom | -Fc |
one archive file | yes, with pg_restore |
no | yes |
| directory | -Fd |
directory of files | yes, with pg_restore |
yes | yes |
A test confirms the limit on parallelism: pg_dump -Fc -j 2 stops immediately in PostgreSQL 18.6 with a message saying that parallel backup is supported only by the directory format. You choose method and level of compression with --compress=<method>[:<level>], for example --compress=zstd:3. Valid methods are gzip, lz4, zstd and none (lz4 and zstd only if your PostgreSQL was built with that support). According to the manual, pg_dumpall itself produces a text script. You cannot selectively restore single databases or tables from such a script with pg_restore. That is the reason to back up the databases one by one with pg_dump.
How do you back up all databases of a cluster in one run?
A short shell script is enough: it backs up the globals first, then asks pg_database for all databases except the templates and calls pg_dump -Fc for each of them. New databases are picked up automatically, without a list to maintain. I tested the logic on 3 December 2026 against PostgreSQL 18.6.
#!/bin/bash
set -euo pipefail
dir=/data/backup/postgresql/$(date +%Y%m%d)
install -d -m 0700 "$dir"
pg_dumpall --globals-only -f "$dir/globals.sql"
psql -At -c "SELECT datname FROM pg_database WHERE NOT datistemplate" |
while read -r db; do
pg_dump -Fc -f "$dir/$db.dump" "$db"
done
On Windows I ran it with mkdir -p instead of install -d -m 0700. The script is meant to run as user postgres, for example from cron with sudo -u postgres. In my test it wrote globals.sql and one .dump file for every non-template database and ended with exit code 0. With set -euo pipefail it stops at the first error instead of carrying on and reporting a half-finished backup as a success.
On purpose, the script has no rotation of old backups, no mail report, no check for free disk space and no parallel runs. They quickly turn the script into a project. If you do not want to build that yourself, my free tool pg-clusterbackup (MIT licence) covers exactly this workflow for all clusters of a host.
Are the dumps of different databases consistent with each other?
No. Each database is consistent in itself, but the snapshots of different databases are not synchronised; the manual says so explicitly. If an application reaches several databases through foreign tables or dblink, dumps taken at different times may not fit together, for example when a record is already in one backup but not yet in the other.
If you need one common point in time across all databases, a dump does not help. The two other backup methods that the manual describes are suited for that: a file system level backup and continuous archiving. Both back up the cluster as a whole, but in my experience they take more effort to set up.
How do you restore a single database?
In three steps: first load globals.sql with psql so that roles and tablespaces exist, then create the database, then run pg_restore on the dump file. Without the first step, pg_restore reports that the role does not exist. I measured this sequence on 3 December 2026 against PostgreSQL 18.6, including the error without the globals.
# 1. Roles and tablespaces first, as a superuser
psql -U postgres -f globals.sql postgres
# 2. Create the database, with the former owner
psql -U postgres -c "CREATE DATABASE app_production OWNER app_owner"
# 3. Restore the dump
pg_restore -U postgres -d app_production app_production.dump
You can combine steps 2 and 3: pg_restore -U postgres -C -d postgres app_production.dump creates the database itself; the connection through -d postgres is then only used to run CREATE DATABASE. I tested that as well. For everything else, the pg_restore manual applies:
- A single table comes back into an empty database with
-t <table>; in my test exactly one table arrived. - The table of contents is shown by
pg_restore -l file.dump. You can edit the output and use it as a selection list with-L. - Replacing existing objects requires
-conpg_restore. A-conpg_dump -Fchas no effect on the archive: I measured that the dump then contained noDROPcommands, whilepg_restore -cproduced two. - Parallel restore works with
-j <count>for custom and directory archives.
You load the file globals.sql as a superuser: according to the pg_dumpall manual, the script needs superuser rights to create roles. It reports "role already exists" for the superuser that the cluster was created with; the manual calls this error harmless. pg_restore is also easiest to run as a superuser: according to the manual, the commands that set owners fail unless the first connection is made by a superuser or by the owner of all objects. With -O you can skip them, and all objects then belong to the user who connects.
Why is a backup only a backup after a test restore?
Because only a restore shows whether the backup is complete and readable. A script can run without any error message and still write the wrong thing. So plan regular test restores on a separate system, and check row counts, owners and roles there, before you rely on the backup in an emergency.
Two bugs in my own tool pg-clusterbackup show how easily a backup goes wrong. Both are in the changelog and have been fixed since version 1.8.1 of 18 November 2026:
- Permissions on the working directory: Since version 1.7.0, the cron run as
rootcreated a working directory with mode0700. Butpg_dumpran as userpostgresand was not allowed to write into it. Every backup on Linux failed with "Permission denied". You only notice that if somebody reads the error message. - Time zone: Without
date.timezoneinphp.ini, PHP on the command line runs in UTC. Between midnight and the UTC offset, the backup ended up in the previous day's folder. That run finished without any error.
The lesson: make every backup run fail loudly, for example with an exit code, a mail or a ping to a monitoring service. Also check regularly that the result is where you expect it and that it can be restored.
Where do logical backups end?
A logical backup only gives you the state at the start of the dump, never an intermediate state. If you want to get back the state at an arbitrary point in time, you need continuous archiving with point-in-time recovery, for which there are tools such as pgBackRest or Barman. Logical dumps complement them; they do not replace them.
In return, dumps can do something physical backups cannot. According to the manual, pg_dump is the only method that works when you move to a different machine architecture, and its output can usually be loaded into newer PostgreSQL versions. That makes dumps a common route for version upgrades and for moving single databases; for very large databases, pg_upgrade and logical replication are alternatives. How to keep a running cluster healthy is covered in PostgreSQL Maintenance — VACUUM, REINDEX, CLUSTER (German); the file system and operating system underneath are covered in The Operating System Beneath PostgreSQL (German).
How do you back up several clusters on one host?
Debian and Ubuntu list all clusters with pg_lsclusters, with version, name, port and status; for every running cluster you start the backup with that cluster's port. Watch the client version: according to the manual, pg_dump does not back up a server whose major version is newer than its own, and stops with "aborting because of server version mismatch".
With mixed versions on one host, use the matching program of each version. On Debian and Ubuntu it lives under /usr/lib/postgresql/<version>/bin/:
bin=/usr/lib/postgresql/16/bin
sudo -u postgres "$bin/pg_dumpall" -p 5433 --globals-only -f /data/backup/postgresql/16-main/globals.sql
To my knowledge, the usual PostgreSQL packages for RHEL ship no such listing tool. There you can read the data directory instead: the file postmaster.pid of a running server contains the port in line 4. I did not run the calls in this section on a system with several clusters.
Frequently Asked Questions (FAQ)
Is pg_dump alone enough to back up a PostgreSQL server?
No. pg_dump backs up exactly one database and contains neither roles nor tablespaces. For a whole cluster you additionally need pg_dumpall --globals-only and one call of pg_dump for every database.
May I run pg_dump while the system is in use?
Yes. According to the manual, pg_dump does not block readers or writers and delivers a snapshot from the start of the dump. The exception is operations that take an exclusive lock, such as most forms of ALTER TABLE. So do not schedule schema changes into the backup window.
Can I restore a single table from a dump?
Yes, if the dump is in custom or directory format. pg_restore -t <table> -d <database> file.dump restores only that table. This does not work with a plain dump; psql always loads it completely.
Why does pg_restore say that the role does not exist?
The dump refers to owners and privileges but does not contain the roles. First load the file globals.sql created with pg_dumpall --globals-only as a superuser, and then pg_restore works.
Can I load a dump into a newer PostgreSQL version?
Usually yes. The manual says that the output of pg_dump can generally be loaded into newer PostgreSQL versions. Take the backup with a pg_dump that is at least as new as the server, otherwise it stops with a version error.
Does a logical dump replace a backup with point-in-time recovery?
No. A dump only represents the state at its start. To recover to an arbitrary point in time you need continuous archiving. Dumps complement it, above all for version changes and single databases.
Related Training, Tools and Articles
- PostgreSQL Administration (German) — practise backup, restore and operation of a cluster on your own system.
- PostgreSQL Security and Hardening (German) — secure roles, privileges and access, including the files of a backup.
- PostgreSQL Maintenance — VACUUM, REINDEX, CLUSTER (German) — regular care of the running cluster.
- The Operating System Beneath PostgreSQL (German) — file system and operating system under the cluster.
- pg-clusterbackup — my free tool for automatic backups of all clusters of a host.
- pg-harden — my tool for security audit and hardening of PostgreSQL with a change plan.
Go deeper in training
If you want to practise backup and restore on your own system: in the seminar PostgreSQL Administration (German) you work on backup and recovery strategies and practise the restore. Training and consulting on this topic are available; the offer is aimed at companies, public authorities and freelancers. I support you on site in the Cologne and Leverkusen area as well as in-house or remotely across Germany. More about consulting is under Database Consulting in the Rhineland (German).
An enquiry to me is non-binding and free of charge; scope and terms are agreed individually afterwards.
In-house training: PostgreSQL Administration
You set up backup, restore and test restore on your own cluster and rehearse the emergency before it happens.
View training PostgreSQL Administration (German)Coaching at your workplace
Together we check on your own systems whether the backup is complete and can really be restored — on site or remotely.
Request consulting or coaching