> ## Documentation Index
> Fetch the complete documentation index at: https://gcore-doc-1894.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Backup and restore PostgreSQL database for Managed PostgreSQL using external utilities

The `pg_dump` and `pg_restore` utilities back up and restore PostgreSQL databases from a remote server without requiring direct filesystem access.

Back up [Gcore Managed PostgreSQL](/cloud/managed-database-postgresql) databases regularly to protect against data loss.

## 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.

<Warning>
  Administrator privileges may be required to install the PostgreSQL utilities.
</Warning>

<Accordion title="Ubuntu 22.04">
  1. Connect to the virtual server.

  2. Download the latest available updates and upgrade `apt`:

  ```sh theme={null}
  $ sudo apt update && sudo apt upgrade
  ```

  3. To install `pg_dump` and `pg_restore` tools, install the Postgres package, which includes them:

  ```sh theme={null}
  $ sudo apt install postgresql postgresql-contrib
  ```
</Accordion>

<Accordion title="CentOS 7">
  1. Connect to the virtual server.

  2. Install the PostgreSQL package:

  ```sh theme={null}
  $ sudo yum install postgresql-server
  ```

  The package contains all required tools.
</Accordion>

<Accordion title="Windows">
  1. Download the [PostgreSQL installer](https://www.enterprisedb.com/downloads/postgres-postgresql-downloads) for the relevant version.

  2. Run the installation process.
</Accordion>

## Get credentials

The backup and restore tools require credentials (`username`, `password`, and `dbname`) to connect to the Gcore Managed PostgreSQL server. Gcore Managed PostgreSQL uses port `5432`. Retrieve credentials from the Customer Portal: [Get credentials](/cloud/managed-database-postgresql/manage-postgresql-servers#get-credentials).

## Back up databases

`pg_dump` backs up a single database. Gcore Managed PostgreSQL does not provide the superuser access required by `pg_dumpall`, so to back up multiple databases, run `pg_dump` separately for each one.

### 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](/cloud/managed-database-postgresql/manage-postgresql-servers#get-credentials). 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

## Restore databases

The restore command depends on the backup format used in the previous section.

**SQL format** (default) — restore with `psql`:

```sh theme={null}
$ psql -U username -W -h hostname -d database_name < database_name.sql
```

**TAR, custom, or directory format** — restore with `pg_restore`:

```sh theme={null}
$ pg_restore -U username -W -h hostname -d database_name backup_path
```

Where `backup_path` is the path to the backup file (`.tar`, `.dump`) or directory created in the previous section.

<Info>
  When restoring to a Gcore Managed PostgreSQL server, `pg_restore` may encounter permission errors on internal system schemas. If the database uses only the `public` schema, restrict the restore to that schema to skip those errors:

  ```sh theme={null}
  $ pg_restore -U username -W -h hostname -d database_name --no-acl --no-owner --schema=public backup_path
  ```

  If the database uses application schemas beyond `public`, omit `--schema=public`. `pg_restore` may report permission errors for system objects; review these errors separately from errors affecting application schemas.
</Info>

Where:

* `username`: the username from the Customer Portal
* `hostname`: the hostname from the Customer Portal
* `database_name`: the name of the database to restore into
* `backup_path`: the backup file (`.tar`, `.dump`) or directory to restore from
