Use the pg_basebackup utility to create a physical full backup of your ApsaraDB RDS for PostgreSQL instance and export the backup files to your local machine or ECS instance.
Prerequisites
Before you begin, ensure that you have:
Added the IP address of your ECS instance or local machine to an IP address whitelist of the RDS instance. For more information, see Configure an IP address whitelist
Installed the same version of PostgreSQL on your ECS instance or local machine as the RDS instance runs
Background
pg_basebackup backs up all data in a PostgreSQL instance at the physical level. The resulting backup files support point-in-time recovery (PITR).
This topic uses CentOS 7 and PostgreSQL 15 as examples.
Usage notes
Use the privileged account of your RDS instance, or an account that has the REPLICATION permission. Other accounts may encounter permission errors.
pg_basebackup cannot back up a single database or database object. For single-database backups, see Create a logical backup for an ApsaraDB RDS for PostgreSQL instance.
Back up an RDS PostgreSQL instance
Log in to your ECS instance or local machine and run the following command:
pg_basebackup -Ft -Pv -Xs -z -D <backupdir> -Z5 -h '<hostname>' -p <port> -U <username> -WThe following table describes each parameter and flag in the command. For the full parameter reference, see pg_basebackup.
Parameter or flag Description -FtOutputs backup files in tar format. -PvShows progress and verbose output during the backup ( -Pfor progress,-vfor verbose).-XsStreams WAL (Write-Ahead Logging) files during the backup using the streaming replication protocol. This ensures the backup is consistent and ready for PITR. -zCompresses the output files with gzip. -Z5Sets the compression level to 5 (range: 1–9, where 9 is maximum compression). -WPrompts for a password. <backupdir>The local directory where backup files are saved. pg_basebackup creates this directory automatically. If the directory already exists and is not empty, the command fails with an error. <hostname>The endpoint of your RDS instance. If your ECS and RDS instances are in the same region under the same Alibaba Cloud account and share the same network type, use the internal endpoint. If both use VPC (Virtual Private Cloud), they must be in the same VPC. In other cases, use the public endpoint. ImportantTo use a public endpoint, you must first apply for one. See Apply for or release a public endpoint.
<port>The port of your RDS instance. <username>A username of your RDS instance. Example:
pg_basebackup -Ft -Pv -Xs -z -D /pg15/backup1/ -Z5 -h pgm-bp****.pg.rds.aliyuncs.com -p 5432 -U test1 -WWhen the
Password:prompt appears, enter the password for the username and press Enter.When
base backup completedis displayed, the backup is complete.
What's next
To restore data from the backup files, follow the steps in Recovering using a continuous archive backup.