pg_dump exports one database as SQL. Sometimes I want the other kind of backup: a copy of the files of the entire server, which is what pg_basebackup makes.
pg_basebackup -D backup -Ft -z -P
The options come straight from the example in the docs. -D backup is the target directory, which is required and has to be empty if it already exists. -Ft writes tar files, -z compresses them with gzip, and -P shows an approximate progress report. The main data directory ends up in base.tar.gz, and by default the write-ahead log is streamed along, so the backup is complete in itself.
The backup is taken while the database keeps running, without affecting other clients. The docs also say that it can serve as the base for point-in-time recovery or to start a standby server.
What you need and what to expect:
- The user needs
REPLICATIONpermission or must be a superuser, andpg_hba.confhas to allow a replication connection. - It always backs up the entire cluster. Backing up one database or one table is not possible, and the docs point to
pg_dumpfor that. - With tar format, you must unpack the tar files yourself before starting a server on them.
- It begins with a checkpoint, and with the default spread mode that can take a while, during which the command looks idle.
For a server that is slow or shared, --max-rate limits how fast data is read. In my opinion a physical backup is the one to schedule for a bigger server, with a dump as a second, portable copy.
Further reading: PostgreSQL documentation: pg_basebackup