All Products
Search
Document Center

ApsaraDB RDS:Restore the data of an ApsaraDB RDS for PostgreSQL instance to a self-managed PostgreSQL instance by using a CSV file or an SQL file

Last Updated:Mar 28, 2026

When you need to move data out of ApsaraDB RDS for PostgreSQL — for example, to decommission an instance, archive data, or sync to a development environment — you can download a snapshot backup as a CSV or SQL file and restore it to a self-managed PostgreSQL instance using a provided Python script.

Choose a restore method

Before you begin, confirm this method fits your situation:

MethodBest forGuide
CSV or SQL file (this guide)Full snapshot restore from a cloud disk instanceThis page
Logical backup with pg_restorePartial restore, such as a single tableRestore data from a logical backup file
Data Transmission Service (DTS)Minimal manual effort, ongoing syncMigrate data from a PostgreSQL database

For a full overview of all restore options, see Overview of data restoration methods.

Prerequisites

Before you begin, make sure that:

  • Your RDS instance uses cloud disks — this method applies only to cloud disk instances

  • Python 3 is installed on the target machine:

    python3 --version
  • The target self-managed PostgreSQL instance does not contain a database with the same name as the one you are importing — if it does, the import fails; drop or rename the conflicting database before proceeding

The steps below use a 64-bit CentOS 7.8 environment as an example. If your operating system differs, use the equivalent commands for your system.

Restore data from a backup file

  1. Log on to the ApsaraDB RDS console and download the backup file of your instance as a CSV or SQL file. For instructions, see Download backup files.

  2. Decompress the downloaded package:

    tar -zxvf <package-name>.tar.gz -C <target-directory>

    For example, to decompress backup.tar.gz into /home/ecs-test-user:

    tar -zxvf backup.tar.gz -C /home/ecs-test-user
  3. (Optional) Confirm that the files were extracted to the target directory:

    ls -al /home/ecs-test-user
  4. Download the PostgreSQL Python script (restore_from_downloads.py) to the same machine.

  5. Grant execute permissions on the script:

    chmod +x ./restore_from_downloads.py
  6. Run the script to restore data to the self-managed PostgreSQL instance:

    ParameterDescriptionExample
    <data-directory>Directory containing the extracted CSV or SQL files/home/ecs-test-user
    <host>Host address of the self-managed PostgreSQL instance127.0.0.1
    <port>Port number5432
    <username>Database account username. If the username contains special characters such as # or spaces, enclose it in double quotation marks (").postgres
    <password>Database account password. If the password contains special characters such as # or spaces, enclose it in double quotation marks (")."#Tes********"
    python3 restore_from_downloads.py <data-directory> <host> <port> <username> <password>

    Example:

    python3 restore_from_downloads.py /home/ecs-test-user 127.0.0.1 5432 postgres "#Tes********"