Skip to main content
This guide explains how to back up and restore PostgreSQL databases using the pg_dump (pg_dumpall) and pg_restore tools. To protect against data loss, back up Gcore Managed PostgreSQL databases regularly.

1. Install external utilities

Install the backup and restore utilities on the virtual or local server. Installation instructions are provided below for Ubuntu, CentOS, and Windows.
Root rights are required to run the commands successfully.
  1. Connect to the virtual server.
  2. Download the latest available updates and upgrade apt:
$ sudo apt update && sudo apt upgrade
  1. To install pg_dump and pg_restore tools, install the Postgres package, which includes them:
$ sudo apt install postgresql postgresql-contrib
  1. Connect to the virtual server.
  2. Install the PostgreSQL package:
$ sudo yum install postgresql-server
The package contains all required tools.
  1. Download the installer of the relevant PostgreSQL version.
  2. Run the installation process.

2. Get your credentials

The backup and restore tools require credentials (username, password, port, and dbname) to connect to the Gcore Managed PostgreSQL server. Credentials are available in the Customer Portal — see Get your credentials for instructions.

3. Back up databases

Back up a specific database

Use one of the following commands depending on the required backup format:
  • Default SQL format: $ pg_dump -U username -W -h hostname -d database_name > database_name.sql
  • TAR format: $ pg_dump -F t -U username -W -h hostname -d database_name > backup_file.tar
  • Custom format: $ pg_dump -F c -U username -W -h hostname -d database_name > backup_file.dump
  • Directory format: $ pg_dump -F d -U username -W -h hostname -d database_name -f backup_dir
Replace username, hostname, and database_name with the actual values from the Customer Portal. For backup_file or backup_dir, specify the name for the backup file or directory. Additional flag details:
  • -W prompts the password before connecting to the PostgreSQL server
  • -F specifies the output format of the backup and can be followed by the following flags:
    • c for custom format
    • d for directory format
    • t for tar

Back up all databases

Run the command:
$ pg_dumpall -U username -W -h hostname > all_pg.sql
Where:
  • all_pg.sql: the output backup file name
  • username: the username from the Customer Portal
  • hostname: the hostname from the Customer Portal

4. Restore databases

Run the command:
$ pg_restore -U username -W -h hostname -d database_name < backup_file.tar
Where:
  • username: the username from the Customer Portal
  • hostname: the hostname from the Customer Portal
  • database_name: the database name from the Customer Portal
  • backup_file.tar: the backup file or directory to restore from