Back Up One Database
sudo -u postgres pg_dump -Fc appdb > appdb.dumpRunning as the postgres system user logs in to the database as its superuser through the local socket, with no password. The redirect is done by your own shell, so the file belongs to you. For a plain SQL file, leave out -Fc:
sudo -u postgres pg_dump appdb > appdb.sqlpg_ALTER TABLE, waits until the dump ends, and queries on that table queue behind it.
Plain or Custom Format
| Format | Option | Restore with |
|---|---|---|
| Plain SQL | -Fp (default) | psql |
| Custom | -Fc | pg_ |
| Directory | -Fd | pg_ |
The custom format is compressed, lets you restore single tables, and can be restored in parallel. Plain SQL is readable and editable. The directory format also allows a parallel dump with -j.
Roles and Global Objects
pg_
sudo -u postgres pg_dumpall --globals-only > globals.sqlThis file contains roles with their password hashes and tablespaces, so protect it like a password file.
A Backup Role with .pgpass
To run backups as a dedicated role over a TCP connection, put its password in ~/.pgpass, never on the command line:
# ~/.pgpass host:port:database:user:password
localhost:5432:appdb:backup:STRONG_PASSWORDchmod 600 ~/.pgpass
pg_dump -h localhost -U backup -Fc appdb > appdb.dumpPostgreSQL ignores the file if its group or others have any access to it. On PostgreSQL 14 and later, GRANT pg_ gives the role read access to every table, view, and sequence; it does not bypass row-level security policies.
Restore with pg_restore
Test into a scratch database, never over the live one:
sudo -u postgres createdb appdb_restoretest
sudo -u postgres pg_restore --no-owner -d appdb_restoretest < appdb.dump
sudo -u postgres psql -d appdb_restoretest -c '\dt'Compare the table list and the row count of a few important tables with the live database. pg_ lists what the archive contains. With a file path instead of a redirect, -j 4 restores with four parallel jobs. A plain dump is restored with psql -d appdb_.
Restoring into an existing database with --clean --if-exists drops each object before recreating it. Dump that database first.
Versions
Use a pg_
A Nightly Cron Job
sudo install -d -o postgres -g postgres -m 700 /var/backups/postgresql#!/bin/bash
# /usr/local/bin/pg-backup, run as postgres
set -euo pipefail
dir=/var/backups/postgresql
day=$(date +%F)
pg_dumpall --globals-only > "$dir/globals-$day.sql"
for db in appdb; do
pg_dump -Fc "$db" > "$dir/$db-$day.dump"
doneMake it executable with sudo chmod 755 /usr/local/bin/pg-backup, then add it to the postgres user's crontab with sudo crontab -u postgres -e:
30 2 * * * /usr/local/bin/pg-backupCron mails or discards the output; to keep a log, see how to create a cron job.
Get the Dumps Off the Server
The dump folder is on the same disk as the database. Copy it to another machine every night with BorgBackup or restic, keep one copy off the provider, and repeat the restore test monthly. For a whole-server plan, see how to back up a Linux server.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations