pig pgbackrest
The pig pgbackrest command (alias pig pb) manages pgBackRest backups and provides low-level restore primitives.
It wraps common pgbackrest operations for a simplified backup management experience. All commands run as the database superuser (default postgres).
For orchestrated point-in-time recovery on managed clusters, prefer pig pitr.
Command Overview
Information Query:
| Command | Alias | Description | Implementation |
|---|---|---|---|
pb info | i | Show backup repository info | pgbackrest info |
pb list | ls | List backup sets, repos, and stanzas | pgbackrest info |
Backup & Restore:
| Command | Alias | Description | Implementation |
|---|---|---|---|
pb backup | b | Create backup | pgbackrest backup |
pb restore | r | Low-level restore primitive | pgbackrest restore |
pb expire | e | Clean up expired backups | pgbackrest expire |
Stanza Management:
| Command | Alias | Description | Implementation |
|---|---|---|---|
pb create | c | Create stanza (first-time setup) | pgbackrest stanza-create |
pb upgrade | u | Upgrade stanza after PG major upgrade | pgbackrest stanza-upgrade |
pb delete | d | Delete stanza (dangerous!) | pgbackrest stanza-delete |
Control Commands:
| Command | Alias | Description | Implementation |
|---|---|---|---|
pb check | ck | Verify backup repository integrity | pgbackrest check |
pb start | up | Enable pgBackRest operations | pgbackrest start |
pb stop | dw | Disable pgBackRest operations | pgbackrest stop |
pb log | l | View logs | latest log snapshot / tail |
Quick Start
Global Options
These options apply to all pig pb subcommands:
| Option | Short | Description |
|---|---|---|
--stanza | -s | pgBackRest stanza name (auto-detected) |
--config | -c | Config file path |
--repo | -r | Repository number (multi-repo scenario) |
--dbsu | -U | Database superuser (default: $PIG_DBSU or postgres) |
Stanza Auto-Detection:
If -s is not specified, pig auto-detects the stanza name from the config file:
- Read the config file (default
/etc/pgbackrest/pgbackrest.conf) - Find sections that do not start with
[global*] - Use the first stanza found
If the config file contains multiple stanzas, pig emits a warning and uses the first one. In that case, specify --stanza explicitly.
Multi-Repo Support:
pgBackRest supports multiple repositories (repo1, repo2, etc.). Use -r to choose the target repository:
Information Commands
pb info
Show detailed backup repository information, including all backup sets and WAL archive status.
Options:
| Option | Short | Description |
|---|---|---|
--raw | -R | Raw output mode (passes through pgBackRest output) |
--raw-output | Raw output format: text, json (only in --raw mode) | |
--set | Show details for a specific backup set |
pb ls
List resources in the backup repository.
Types:
| Type | Description | Data Source |
|---|---|---|
| backup | List all backup sets (default) | pgbackrest info |
| repo | List configured repos | Parse pgbackrest.conf |
| stanza | List all stanzas | Parse pgbackrest.conf |
Backup Commands
pb backup
Create a physical backup. Backups can only run on the primary instance.
Options:
| Option | Short | Description |
|---|---|---|
--force | -f | Skip primary role check |
Backup Types:
| Type | Description |
|---|---|
| (empty) | Auto mode: full if no backup exists, otherwise incremental |
| full | Full backup: backup all data |
| diff | Differential backup: changes since the last full backup |
| incr | Incremental backup: changes since the last backup of any type |
Primary Check:
Before running a backup, the command automatically checks whether the current instance is primary. If it is a replica, the command exits with an error. Use --force to skip this check.
pb expire
Clean up expired backups and WAL archives according to the retention policy.
Options:
| Option | Short | Description |
|---|---|---|
--set | Delete a specific backup set | |
--plan | Preview cleanup plan only, do not delete backups | |
--yes | -y | Skip confirmation prompt when used with --set |
Retention Policy:
Retention policy is configured in pgbackrest.conf:
Restore Commands
pb restore
Restore from backup with recovery target support.
At least one recovery target (-d/-I/-t/--name/--lsn/--xid) must be specified explicitly. Without parameters, help is shown.
Recovery Target Options:
| Option | Short | Description |
|---|---|---|
--default | -d | Restore to end of WAL stream (latest data) |
--immediate | -I | Restore to backup consistency point |
--time | -t | Restore to specific timestamp |
--name | Restore to named restore point | |
--lsn | Restore to specific LSN | |
--xid | Restore to specific transaction ID |
Backup Set and Other Options:
| Option | Short | Description |
|---|---|---|
--set | -b | Restore from a specific backup set (can combine with target) |
--data | -D | Target data directory |
--exclusive | -X | Exclusive mode: stop before target |
--target-action | Action after reaching recovery target: pause/promote/shutdown | |
--target-timeline | -T | Recovery timeline: latest/current/N/0xN |
--plan | Preview restore plan only, do not execute | |
--yes | -y | Skip interactive y/yes confirmation |
Native args after -- | Pass through pgBackRest restore args, for example -- --delta |
Combination Rules: --target-action cannot be used with --default, because --default already means recovery to the end of the WAL stream. --exclusive/-X must be used with an explicit stop target: --time, --lsn, or --xid.
Native pgBackRest restore args after -- cannot override recovery targets, lifecycle, data directory, repository, config, or selection semantics already managed by Pig. Use Pig’s first-class options for those semantics. Tablespace/link migration options such as --tablespace-map, --link-map, and --link-all can still be passed through.
Time Formats:
Supports multiple time formats and auto-completes timezones (including non-integer-hour zones such as +05:30):
| Format | Example | Description |
|---|---|---|
| Full format | 2025-01-01 12:00:00+08 | Complete timestamp with timezone |
| Date only | 2025-01-01 | Auto-completes to 00:00:00 that day (current timezone) |
| Time only | 12:00:00 | Auto-completes to today (current timezone) |
Restore Flow:
- Validate parameters and environment
- Check that PostgreSQL is stopped
- Show restore plan and wait for interactive
y/yesconfirmation - Execute pgbackrest restore
- Provide post-restore guidance
Important: Stop PostgreSQL before restore. If the PGDATA is managed by Patroni, use pig pitr to orchestrate Patroni, PostgreSQL, and pgBackRest:
Stanza Management Commands
pb create
Initialize a new stanza. Must run before the first backup.
Options:
| Option | Short | Description |
|---|---|---|
--no-online | Create when PostgreSQL is not running | |
--force | -f | Force create |
pb upgrade
Update stanza after a PostgreSQL major version upgrade.
Options:
| Option | Description |
|---|---|
--no-online | Upgrade when PostgreSQL is not running |
Use Case:
After a PostgreSQL major version upgrade (for example, 16 -> 17), run this command to update stanza metadata.
pb delete
Delete a stanza and all its backups.
Options:
| Option | Short | Description |
|---|---|---|
--plan | Preview delete plan only, do not execute | |
--yes | -y | Skip interactive y/yes confirmation |
Warning: This is a destructive and irreversible operation. All backups will be permanently deleted.
The command includes multiple safety mechanisms:
- In text mode, it requires interactive
y/yesconfirmation unless--yesis specified - Structured output mode requires explicit
--yes - If the config file contains multiple stanzas and
--stanzais not specified, pig refuses to auto-select a deletion target
Control Commands
pb check
Verify backup repository integrity and configuration.
This command checks:
- WAL archive configuration correctness
- Repository accessibility
- Stanza configuration validity
pb start
Enable pgBackRest operations.
Use this command after pb stop to resume normal operations.
pb stop
Disable pgBackRest operations for maintenance.
Options:
| Option | Short | Description |
|---|---|---|
--force | -f | Terminate running operations |
Use Case:
Use this command during system maintenance to prevent new backup operations from starting.
Log Commands
pb log
View pgBackRest log files. The log directory is read from pgBackRest config log-path first, and falls back to /pg/log/pgbackrest/ when not configured. The parent command shows the latest log snapshot by default; use tail or -f for live follow. Only pb log and pb log show support -o json JSONL output; log snapshots do not support yaml or json-pretty, and follow/tail does not support structured output.
Subcommands:
| Subcommand | Aliases | Description |
|---|---|---|
| list | ls | List log files |
| show | cat, c | Show latest log content |
| tail | t, f, follow | Real-time follow latest log |
Options:
| Option | Short | Default | Description |
|---|---|---|---|
--lines | -n | 50 | Number of lines to show |
--follow | -f | false | Makes parent command pb log follow; no-op in pb log tail because tail always follows |
Permission Handling:
If the current user does not have permission to read the log directory, the command automatically retries with sudo.
Design Notes
Command Execution:
All pig pb commands run as the database superuser (DBSU), because pgBackRest needs access to PostgreSQL data files and WAL archives.
Execution logic:
- If the current user is DBSU: execute directly
- If the current user is root: use
su - postgres -c "..."to execute - Other users: use
sudo -inu postgres -- ...to execute
Relationship with pgbackrest:
pig pb is not a complete wrapper for pgbackrest; it is a higher-level abstraction for common operations:
- Auto-detect stanza name without specifying it every time
- Auto-check primary role before backup
- Show restore plan and require interactive
y/yesconfirmation before restore - Provide user-friendly time format input
- Provide post-restore guidance
For full pgbackrest functionality, use the pgbackrest command directly.
Default Configuration Paths:
| Config | Default |
|---|---|
| Config file | /etc/pgbackrest/pgbackrest.conf |
| Log directory | /pg/log/pgbackrest |
| Data directory | pg1-path from config, or $PGDATA, or /pg/data |
Security Considerations:
pb deleterequires interactivey/yesconfirmation when--yesis not specified, and--stanzamust be explicit under multi-stanza configspb restorerequires an explicit recovery target, validates--time, and requires interactivey/yesconfirmation when--yesis not specifiedpb backupchecks primary role by default to prevent running on a replicapb log taildoes not support structured output; usepb log show -n N -o jsonwhen a JSONL snapshot is required
Platform Support:
This command is designed for Linux systems and depends on Pigsty’s default directory layout.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)