Help Center> Relational Database Service> User Guide> Working with RDS for PostgreSQL> Backups and Restorations> Restoring from Backup Files to a Self-Built PostgreSQL Database

Restoring from Backup Files to a Self-Built PostgreSQL Database

You can download backup files by referring to Downloading a Backup File and restore data from them.

From July 21, 2020 to November 3, 2020, HUAWEI CLOUD has optimized the decompression of PostgreSQL backup packages. You can decompress PostgreSQL backup packages without using any decompression tools. Pay attention to the following when decompressing PostgreSQL backup packages:

  • For backup packages generated before July 21, 2020, you need to use a tool to decompress them by referring to Procedure (Decompression with Tools).
  • For backup packages generated after November 3, 2020, you can decompress them directly without any tools by referring to Procedure (Direct Decompression).
  • Due to time differences, whether direct decompression is available vary in different regions. For backup packages generated between July 21, 2020 and November 3, 2020, you can try to directly decompress them first. If the decompression fails, then use a tool to decompress them.
  • If you have any questions on direct decompression, submit a service ticket by choosing Service Tickets > Create Service Ticket on the management console.

Prerequisites

Decompression with tools:

  1. A tool for decompressing TAR files must be installed in a Unix-like system.
  2. Either python2.7 or python3 is used.
  3. The following third-party packages need to be installed: lz4, gzip, and shutil.

Procedure (Direct Decompression)

  1. Prepare a local directory for storing tablespaces.
  2. Stop the database server.
  3. Store the following configuration files in the data directory of the local database: postgresql.conf, pg_hba.conf, and recovery.done.
  4. Clear the data directory of the local database.
  5. Run the following command to decompress backup data to a temporary directory:

    tar -zxf src_file -C target_dir

    Variables in the command are described as follows:

    src_file: indicates the directory where PostgreSQL full backup files are stored.

    target_dir: indicates the directory to which data is restored and it has been prepared in step 1. The directory must be empty. If the directory does not exist, the system automatically creates it.

    The following directories are generated after the decompression:

    • base: stores full files.
    • pg_wal (if the PostgreSQL version is 9.x, the directory is pg_xlog): stores incremental files.
    • A number of N tablespace directories named with digits files (if the original backup contains tablespace files)

  6. Copy the files in 5 to a specified directory of the local database.

    1. Copy all the decompressed files in the base directory to the data directory of the local database, and then replace the three files in the data directory of the local database with the configuration files in step 3.
    2. Copy the files in the decompressed pg_wal directory (if the PostgreSQL version is 9.x, the directory is pg_xlog) to the pg_wal (or pg_xlog directory) under the data directory of the local database.
    3. If the original backup contains tablespace files, modify the corresponding tablespace soft links in data\tablespace_map.

  7. Reboot the database and wait until the database restoration is complete.

Procedure (Decompression with Tools)

  1. Prepare a local directory for storing tablespaces.
  2. Stop the database server.
  3. Store the following configuration files in the data directory of the local database: postgresql.conf, pg_hba.conf, and recovery.done.
  4. Clear the data directory of the local database.
  5. Download the decompression tool.
  6. Run the following command to decompress backup data to a temporary directory:

    python restore_obs_file.py src_file target_dir

    Variables in the command are described as follows:

    src_file: indicates the directory where PostgreSQL full backup files are stored.

    target_dir: indicates the directory to which data is restored. The directory has been prepared in 1. The directory must be empty. If the directory does not exist, the system automatically creates it.

    The following directories are generated after the decompression:

    • data: stores full backup data. The recovery.done file has been deleted.
    • xlog: stores incremental backup data.
    • tblspc: stores tablespace directory files (if the original backup contains tablespace files).

  7. Copy the files in 6 to the specified directory of the local database.

    1. Copy all the decompressed files in the data directory to the data directory of the local database, and then replace the three files in the data directory of the local database with the configuration files saved in step 3.
    2. Copy the decompressed files in the xlog directory to the pg_xlog or pg_wal folder in the data directory of the local database. (The folder names vary according to database versions.)
    3. Move the decompressed tablespace folder (if any) in the tblspc directory to the tablespace directory created in step 1 and modify the soft link of the corresponding tablespace in data\tablespace_map.

  8. Reboot the database and wait until the database restoration is complete.