# How to Back Up and Restore a PostgreSQL Database in xCloud

> xCloud site backups include your managed PostgreSQL database and restore it in place. Turn on Take Database Backup, check the banner, or export it with pgAdmin or pg_dump.

**xCloud site backups include your managed PostgreSQL database.** Each backup of a PostgreSQL site saves a full dump of its database next to its files, in every backup destination, and **Restore** puts the database back in place. This guide shows how to check that your backups include the database, how to restore it, and how to make your own copy with **pgAdmin** or **pg_dump** when you want a file you keep yourself.

Running the separate [PostgreSQL one-click app](/postgresql-hosting/) instead? It is a Docker app, so use [Docker app backups](/docs/backup-and-restore-docker-apps/): included with a Cloud VPS; a server you connect needs a paid plan.

## Back up PostgreSQL with xCloud site backups

Site backups work the same way for a PostgreSQL site as for a MySQL site: set up a destination in **Backup Settings**, then take backups by hand or on a schedule. For destinations, schedules and retention, see [Site backups in xCloud](/docs/site-backups-in-xcloud/).

### Step 1: Keep Take Database Backup selected

Open your site and go to **'Backup' → 'Backup Settings'**. On the **Remote Backup** or **Local Backup** tab, check that **Database Backup** is set to **Take Database Backup**, then click **Save Settings**. The card names the database that will be saved.

![Database Backup set to 'Take Database Backup' in Backup → Backup Settings for a PostgreSQL site](/_landing/docs/back-up-and-restore-postgresql-in-xcloud-image-2.png)

Database is on by default for PostgreSQL sites, as it is for MySQL sites. Leave it on: with **Do Not Take Database Backup**, that destination saves files only.

### Step 2: Check the banner

Go to **'Backup' → 'Previous Backups'**. A green banner at the top says **Backups include this site's PostgreSQL database.** when at least one destination saves it.

![The banner 'Backups include this site's PostgreSQL database.' in Backup → Previous Backups](/_landing/docs/back-up-and-restore-postgresql-in-xcloud-image-3.png)

If no destination saves the database, the banner says **This site's PostgreSQL database is not in any backup yet. Turn on Database in a backup destination to include it.** Go back to Step 1 and turn on **Take Database Backup**.

### Step 3: Take a backup

Click **Backup Now** on the **Local Backup** or **Remote Backup** tab, or turn on **Automatic Backup** in **Backup Settings** to run backups on a schedule.

**Expected result:** the new backup shows in the list with its files and a database file marked **database: full dump**. xCloud saves the whole database in every backup, on every destination: Local, S3-compatible storage, Google Drive, pCloud and SFTP. An **Incremental** backup saves only the files that changed, but still the full database each time.

### If the backup fails

If xCloud can't save the database, the whole backup fails and you get an email. This usually means PostgreSQL is stopped. You may see either of these messages:

> PostgreSQL database size check failed. Is the PostgreSQL service running?

> The PostgreSQL container is not running, so the database was not backed up. Start or restart it from the server's PostgreSQL page.

The first one is the one you will see most often.

To fix it, restart PostgreSQL (see [Restart PostgreSQL and read its logs](/docs/install-managed-postgresql-and-pgadmin-in-xcloud/#restart-postgresql-and-read-its-logs)), wait until it shows **Ready**, then click **Backup Now** again. For other backup errors, see [Backup failure troubleshooting](/docs/how-to-solve-automatic-site-backup-failures/).

## Restore PostgreSQL from a site backup

1. Go to **'Backup' → 'Previous Backups'** and select the backup you want.
2. Open the **⋯** menu next to the backup and click **Restore Backup**.
3. Under **Select the components to restore**, choose **Both File and Database** or **Only Database**.
4. Type `RESTORE` to confirm and click **Restore**.

**Expected result:** the site's database holds the data from the backup. Before it imports the backup, xCloud saves a copy of the current database. If the restore fails, that copy stays on the server and the error message shows where it is. After a restore that works, the copy is removed, so click **Backup Now** first if you might need today's data.

Things to know:

- **Restore in place only.** A PostgreSQL backup restores into its own site. **Restore to Another Site** and **Restore to New Site** answer **A PostgreSQL site can only be restored in place, from its own backups.** To copy the data to another site, use pgAdmin or pg_dump below.
- **PostgreSQL must be running.** If it is stopped, the restore stops before it changes anything and asks you to start or restart PostgreSQL, then restore again.
- **Only backups that hold a database file can restore the database.** Check that the backup shows **database: full dump** before you choose **Only Database**.

To check the restored data from your computer, [connect to PostgreSQL with an SSH tunnel](/docs/connect-to-postgresql-with-an-ssh-tunnel-in-xcloud/) and open the database in DBeaver, TablePlus or `psql`.

## Make your own copy with pgAdmin or pg_dump

Site backups cover day-to-day recovery. Export the database yourself when you want a file to keep on your own computer, move the data to another site, or restore a single database by hand.

### Before you start

- **Managed PostgreSQL is Ready** on the server. See [How to Install Managed PostgreSQL and pgAdmin in xCloud](/docs/install-managed-postgresql-and-pgadmin-in-xcloud/).
- **For the pgAdmin method:** pgAdmin is enabled on the server (Step 3 of that guide).
- **For the SSH method:** a sudo user with SSH access to the server. See [Sudo Users and Site Users in xCloud](/docs/usudo-users-and-site-users-in-xcloud/) and [How to Generate and Add SSH Keys in xCloud](/docs/how-to-generate-and-add-ssh-keys-in-xcloud/).
- **The database name.** Open your site and go to **Environment** (under **Laravel** or **Node.js** in the site menu). The database name is the value of `DB_DATABASE` (the database user is `DB_USERNAME`). A **PHP App** site has no Environment page; open its `.env` file in **File Manager** instead.

### Back up with pgAdmin

#### 1. Open pgAdmin

Open your server and go to **'Management' → 'Database'**. On the **pgAdmin** card, click **Open pgAdmin**. pgAdmin opens over HTTPS in a new tab.

![Click 'Open pgAdmin' on the pgAdmin card in Management → Database](/_landing/docs/back-up-and-restore-postgresql-in-xcloud-image-1.png)

Sign in with the pgAdmin **Email** and **Password**. Click **Show credentials** on the same card to see them.

#### 2. Find your database

In pgAdmin's **Object Explorer**, expand **Servers** → **PostgreSQL** → **Databases**. xCloud adds the **PostgreSQL** server to pgAdmin for you, so you don't need to register it. Your site databases are listed under **Databases**.

#### 3. Start the backup

Right-click the database and choose **Backup...**. In the dialog, enter a **Filename** (for example `mysite.dump`), keep **Format** set to **Custom**, and click **Backup**. pgAdmin shows a notice when the backup finishes.

#### 4. Download the dump file

pgAdmin saves the file in its own storage on the server. To copy it to your computer, open **Tools** → **Storage Manager**, select the file and download it. Keep a copy off the server: if the server is lost, a dump stored only on it is lost too.

### Restore with pgAdmin

1. In **Tools** → **Storage Manager**, upload the dump file if it isn't there yet.
2. In **Object Explorer**, right-click the database you want to restore into and choose **Restore...**.
3. Keep **Format** as **Custom or tar**, pick the file in **Filename**, and click **Restore**.

To restore into an empty database, create it first in pgAdmin (right-click **Databases** → **Create** → **Database...**). Restoring into a database that already has tables can fail on objects that already exist. In that case, on the **Query Options** tab of the **Restore** dialog, turn on **Clean before restore**, which drops each object before it is restored, or use the SSH method below.

### Back up with pg_dump over SSH

PostgreSQL runs in a Docker container on your server, so you run `pg_dump` inside that container. PostgreSQL can only be reached from the server itself, not from the internet.

#### 1. Find the PostgreSQL container

SSH into the server as your sudo user, then run:

```bash
sudo docker ps --filter name=postgresql- --format '{{.Names}}'
```

**Expected result:** one name that starts with `postgresql-`. The rest of the name differs on every server, so copy yours. The commands below store it in a variable:

```bash
PG_CONTAINER=$(sudo docker ps --filter name=postgresql- --format '{{.Names}}')
```

#### 2. Dump the database

Replace `YOUR_DATABASE` with the database name and run:

```bash
sudo docker exec "$PG_CONTAINER" pg_dump -U postgres -Fc YOUR_DATABASE > YOUR_DATABASE.dump
ls -lh YOUR_DATABASE.dump
```

**Expected result:** a `.dump` file in your current folder. `-Fc` writes PostgreSQL's custom format, which `pg_restore` and pgAdmin's **Restore...** both read. For a plain SQL file you can read, leave out `-Fc` and name the file `.sql`.

#### 3. Copy the file off the server

From your own computer, download the file, for example with `scp`:

```bash
scp YOUR_SUDO_USER@SERVER_IP:YOUR_DATABASE.dump .
```

### Restore with pg_restore over SSH

From your own computer, upload the dump file to the server:

```bash
scp YOUR_DATABASE.dump YOUR_SUDO_USER@SERVER_IP:
```

Then SSH into the server, set `PG_CONTAINER` as in "Find the PostgreSQL container" above, and run:

```bash
sudo docker exec -i "$PG_CONTAINER" pg_restore -U postgres -d YOUR_DATABASE --clean --if-exists < YOUR_DATABASE.dump
```

**Expected result:** the command finishes without errors and your tables hold the data from the dump. `--clean --if-exists` drops each object in the dump before it creates it again, so **the data in those tables now is replaced**. The tables keep their original owner, so the site's role can still use them.

For a plain `.sql` dump, use `psql` instead:

```bash
sudo docker exec -i "$PG_CONTAINER" psql -U postgres -d YOUR_DATABASE < YOUR_DATABASE.sql
```

**Expected result:** `psql` prints a line for each statement it runs (such as `CREATE TABLE` and `COPY`) with no `ERROR` lines, and your tables hold the data from the dump.

A plain dump made without `--clean` fails on tables that already exist. Restore it into an empty database, or make the dump with `pg_dump --clean --if-exists` so it drops each object before it creates it again.

## Limits to know

| Area | What happens |
|---|---|
| xCloud site backups | Include the full PostgreSQL database in every backup when **Database** is on |
| Restore | In place only, into the site the backup came from |
| Restore to another site or a new site | Not available for PostgreSQL sites. Use a pgAdmin or pg_dump export to move the data |
| Clone, migrate, staging, snapshots | Not supported for PostgreSQL sites yet, so a dump is how you move the data |
| Deleting a site | The site's database and role are kept. Take a dump before you remove them in pgAdmin |

For MySQL sites and site files in general, see [Site backups in xCloud](/docs/site-backups-in-xcloud/).

## Frequently asked questions

### Do xCloud site backups include my PostgreSQL database?

Yes. When **Database Backup** is set to **Take Database Backup** in Backup Settings, every site backup saves a full copy of the site's PostgreSQL database next to its files, and you can restore it in place from Previous Backups.

### What happens if PostgreSQL is stopped when a backup runs?

The backup fails and you get the failure email, instead of finishing without the database. Start or restart PostgreSQL from the server, then click Backup Now.

### Can I restore a PostgreSQL site's backup into another site?

No. A PostgreSQL site can only be restored in place, from its own backups. To copy the data somewhere else, export it with pgAdmin or pg_dump.

### Does restoring a dump overwrite the data that is there now?

Yes. A restore replaces the data in the database with the data in the backup. xCloud keeps a copy of the current database only if the restore fails, so take a backup first if you might need the current data.

If you run into any issues backing up or restoring PostgreSQL, feel free to reach out to our [support team](/docs/access-built-in-support-portal-in-xcloud/).
