> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Backup, Restores, & Snapshots

<Warning>
  Important: We are not able to provide support or guidance for unmanaged Postgres. We now offer [Fly.io Managed Postgres](/mpg) , our fully-managed database service that handles all aspects of running production PostgreSQL databases.
</Warning>

An important part of running any production db-backed application is making sure your data is backed up and safe, and if the unspeakable happens, it's straightforward to restore data and get back up and running.

Postgres databases on Fly.io are treated as Fly Apps, which you can [read more about in the docs](/postgres). What that means is backing up data is an exercise in taking snapshots of the Postgres app's volumes, then restoring the snapshots to a new database server, verifying the restoration, and connecting the application to the restored database.

## Finding the database app instance

If you set up your application using `fly launch`, the name of your database app might be `<app-name>-db`, or you might have given it a name during the initial configuration. To find the name of your Postgres app, run:

<CodeGroup>
  ```bash cmd theme={null}
  fly postgres list
  ```

  ```output output theme={null}
  NAME                       OWNER    STATUS  LATEST DEPLOY
  my-app-db       personal running 37m11s ago
  ```
</CodeGroup>

Look for your database app under the `NAME` column. In this example, `my-app-db` is the value to use for `<postgres-app-name>` in the following commands.

## Listing snapshots

Fly.io performs daily storage-based snapshots of each of your provisioned volumes. These snapshots can be used to restore your dataset into a new Postgres application.

Snapshots are volume specific, so you need to identify the volume whose snapshot you want to restore from. You can list your volumes by running the `volumes list` command with your Postgres app name.

<CodeGroup>
  ```bash cmd theme={null}
  fly volumes list -a <postgres-app-name>
  ```

  ```output output theme={null}
  ID                   NAME    SIZE REGION ATTACHED VM CREATED AT
  vol_x915grn008vn70qy pg_data 10GB sjc    b780ce3d    2 weeks ago
  vol_ke628r677pvwmnpy pg_data 10GB sjc    359d0e24    2 weeks ago
  ```
</CodeGroup>

The number of volumes varies depending on how many database replicas were elected when provisioning the database. One primary database and one replica yields 2 volumes.

Once you have identified which volume to target, you can list its snapshots:

<CodeGroup>
  ```bash cmd theme={null}
  fly volumes snapshots list <volume-id>
  ```

  ```output output theme={null}
  ID                  SIZE   CREATED AT
  vs_2AjJ4lGqQwDbRfxm 29 MiB 2 hours ago
  vs_BAARBQxZKl6JKU04 27 MiB 1 day ago
  vs_OPQXXna6kA2Qnhz8 26 MiB 2 days ago
  ```
</CodeGroup>

The values under the `ID` columns are what will be used to restore a snapshot.

## Identifying your Postgres image version

Depending on when you created your Postgres cluster, it may be running an older image than the default for newly created clusters. Different Postgres major versions may not be fully compatible, so it's important to use the same version for your restored cluster.

To see your Postgres image and version, run `fly image show`.

<CodeGroup>
  ```bash cmd theme={null}
  fly image show -a <postgres-app-name>
  ```

  ```output output theme={null}
  MACHINE ID      REGISTRY                REPOSITORY      TAG     VERSION DIGEST                        LABELS                                                                          
  e286004f696700  registry-1.docker.io    flyio/postgres  14.6    v0.0.41 sha256:3c25db96357a78e827ca7d fly.app_role=postgres_clusterfly.pg-version=14.6-1.pgdg110+1fly.version=v0.0.41
  ```
</CodeGroup>

The values under the `REPOSITORY` and `TAG` columns reference the image you'll use to restore the snapshot.
Legacy postgres images use the `flyio/postgres` repository, while new Postgres Flex images use the `flyio/postgres-flex` repository. Join the two values with a colon to form the image reference: in the above example, the machine is running a legacy `flyio/postgres:14.6` image.

## Restoring from a snapshot

To restore a Postgres application from a snapshot, simply specify the `--snapshot-id` argument and the `--image-ref` argument when running the `create` command as shown below:

<CodeGroup>
  ```bash cmd theme={null}
  fly postgres create --snapshot-id <snapshot-id> --image-ref <repository>:<tag>
  ```

  ```output output theme={null}
  ? App Name: my-app-db-restored
  Automatically selected personal organization: Awesome Person
  ? Select region:  [Use arrows to move, type to filter]
  ? Select region: sjc (San Jose, California (US))
  ? Specify the initial cluster size: 2
  ? Select VM size: shared-cpu-1x - 256
  Creating postgres cluster my-app-db-restored in organization personal
  Postgres cluster my-app-db-restored created
    Username:    postgres
    Password:    <redacted>
    Hostname:    my-app-db-restored.internal
    Proxy Port:  5432
    PG Port: 5433
  Save your credentials in a secure place, you won't be able to see them again!
  ```
</CodeGroup>

This provisions and launches a new Fly Postgres database server with the specified snapshot and the same image as your existing database.

<Note>
  **Important:** The size of the volume provisioned for the new Fly Postgres application must be equal to or greater than the volume from where the snapshot was taken.
</Note>

## Connect the Restored Database

Detach the web application from the current Postgres cluster:

```bash theme={null}
fly postgres detach my-app-db
```

Then attach it to the new cluster:

```bash theme={null}
fly postgres attach my-app-db-restored
```

Now your application is pointing at the restored database.
