Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Back Up One Database

sudo -u postgres pg_dump -Fc appdb > appdb.dump

Running 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.sql

pg_dump does not block normal reads and writes. It holds shared locks on the tables it dumps, so a command that needs an exclusive lock, such as most forms of ALTER TABLE, waits until the dump ends, and queries on that table queue behind it.

Plain or Custom Format

FormatOptionRestore with
Plain SQL-Fp (default)psql
Custom-Fcpg_restore
Directory-Fdpg_restore

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_dump saves one database, not the roles that own it. Save those separately:

sudo -u postgres pg_dumpall --globals-only > globals.sql

This 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_PASSWORD
chmod 600 ~/.pgpass
pg_dump -h localhost -U backup -Fc appdb > appdb.dump

PostgreSQL ignores the file if its group or others have any access to it. On PostgreSQL 14 and later, GRANT pg_read_all_data TO backup; 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_restore -l appdb.dump 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_restoretest < appdb.sql.

Restoring into an existing database with --clean --if-exists drops each object before recreating it. Dump that database first.

Versions

Use a pg_dump of the same major version as the server, or newer: pg_dump refuses to dump a server newer than itself. When you move to a new server version, run the newer pg_dump against the old server. A dump loads into newer servers; loading it into an older server is not supported and may need hand edits.

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

Make 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-backup

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

Was this answer helpful?
Loading...