SoFunction
Updated on 2025-04-06

Summary of commonly used backup methods in PostgreSQL

PostgreSQL provides a variety of ways to backup, suitable for scenarios with different needs. Common backup methods are as follows:

1. Logical backup (pg_dump and pg_dumpall)

1.1 Backup a single database using pg_dump

pg_dumpis a built-in logical backup tool for PostgreSQL, which can export databases as SQL scripts or binary files.

Backup command

  • Backup as SQL files

    pg_dump -U username -d database_name -F p -f /path/to/
    

    Parameter description:

    • -U: username.
    • -d: Database name.
    • -F p: The output is in plain text format.
    • -f: Specify the backup file path.
  • Backup as compressed files

    pg_dump -U username -d database_name -F c -f /path/to/
    

    Parameter description:

    • -F c: Custom format.
    • The compression format can be usedpg_restoreRestore.

Restore command

  • For SQL files:
    psql -U username -d new_database_name -f /path/to/
    
  • For compressed files:
    pg_restore -U username -d new_database_name /path/to/

1.2 Use pg_dumpall to back up all databases

pg_dumpallUsed to back up the entire PostgreSQL instance (including all databases and global objects, such as roles, permissions, etc.).

Backup command

pg_dumpall -U username -f /path/to/backup_all.sql

Restore command

psql -U username -f /path/to/backup_all.sql

2. Physical backup (pg_basebackup)

2.1 Using pg_basebackup

pg_basebackupIt is a physical backup tool provided by PostgreSQL, suitable for complete binary backup of databases.

Backup command

pg_basebackup -U replication_user -D /path/to/backup_directory -Fp -Xs -P

Parameter description:

  • -U: Backup user (need to configurereplicationpermission).
  • -D: Target backup directory.
  • -Fp: File mode (Plain format).
  • -Xs: Contains WAL logs.
  • -P: Show progress.

Restore steps

  • Stop the PostgreSQL service:
    systemctl stop postgresql
    
  • Copy the backup data to the PostgreSQL data directory:
    cp -r /path/to/backup_directory/* /var/lib/pgsql/data/
    
  • Start the PostgreSQL service:
    systemctl start postgresql
    

3. Archive log backup (WAL log)

Archive log backup is suitable for scenarios where point-in-Time Recovery (PITR) is required.

step

  • Configure archive mode
    edit

    archive_mode = on
    archive_command = 'cp %p /path/to/archive/%f'
    
  • Restart PostgreSQL

    systemctl restart postgresql
    
  • Regularly backup archive logs
    Regularly copy configuration scripts/path/to/archive/Table of contents.

  • Recover using logs and backup
    When recovery is required, perform recovery in combination with the initial physical backup and archive log files.

4. Other backup tools

4.1 Barman

Barman is a community-maintained PostgreSQL dedicated backup tool that supports physical backup and point-in-time recovery.

Backup command

barman backup server_name

Recovery command

barman recover server_name backup_id /path/to/restore_directory

4.2 WAL-G

WAL-G is a modern backup tool for PostgreSQL, which supports incremental backup and rapid recovery, and is often used in cloud environments.

Best Practices

  • Regular backup

    • Use every day/weeklypg_dumporpg_basebackup
    • Implement automated backup scripts.
  • Enable Archive Logs

    • Ensure that WAL logs can be used for point-in-time recovery.
  • Store backups in multiple places

    • Local storage + cloud storage (such as S3, Google Cloud Storage).
  • Test restore process

    • Regularly verify the integrity of backup files.
    • Drill disaster recovery.
  • Monitor backups

    • Combined with monitoring tools (such as Zabbix, Prometheus) to monitor backup progress and status.

Regular backup of PostgreSQL databases in these ways can maximize the security and recovery of data.

Summarize

This is the end of this article about the common backup methods of PostgreSQL. For more related content on PostgreSQL backup methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!