pig pgbackrest
Use pig pgbackrest, or its alias pig pb, to manage pgBackRest backups and run low-level restores.
It simplifies common pgbackrest operations. All commands run as the database superuser, which defaults to postgres.
For managed-cluster point-in-time recovery with lifecycle control, use 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 configuration has multiple stanzas, pig warns and selects the first.
In this 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 a backup, the command checks whether the instance is primary.
If it is a replica, the command returns 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 a backup to a recovery target.
Specify at least one target explicitly: -d/-I/-t/--name/--lsn/--xid.
Without parameters, the command shows help.
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: do not combine --target-action with --default, which recovers to the end of the WAL stream.
Use --exclusive/-X only with an explicit stop target: --time, --lsn, or --xid.
Native pgBackRest arguments after -- cannot override parameters that Pig manages.
These include recovery targets, lifecycle, data directory, repository, configuration, and selection parameters.
Use Pig’s own options for those settings.
You can pass tablespace and link migration options, such as --tablespace-map, --link-map, and --link-all.
Time Formats:
The command accepts multiple time formats and completes missing timezones. It also supports 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 restoration.
If Patroni manages PGDATA, use pig pitr to control 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 operation permanently deletes all backups. You cannot reverse it.
The command includes multiple safety mechanisms:
- In text mode, confirm with interactive
y/yes, or specify--yes. - In structured output mode, specify
--yesexplicitly. - If the configuration has multiple stanzas, specify
--stanza. Pig refuses to select the deletion target automatically.
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 command reads log-path from pgBackRest configuration, or uses /pg/log/pgbackrest/ as a fallback.
By default, the parent command shows the latest log snapshot. Use tail or -f to follow new entries.
Only pb log and pb log show support JSONL output with -o json.
Snapshots do not support yaml or json-pretty. Follow and tail do 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).
PgBackRest requires 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 simplifies common pgbackrest operations through these features:
- 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 unless you specify--yes. With multiple stanzas, specify--stanzaexplicitly.pb restorerequires an explicit recovery target and validates--time. It requires interactivey/yesconfirmation unless you specify--yes.pb backupchecks the primary role by default to prevent backups on a replica.pb log taildoes not support structured output. Usepb log show -n N -o jsonfor a JSONL snapshot.
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)