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

# Restore a managed Postgres database to a point in time

> Undo a bad delete on Reliant hosting: declare a new managed Postgres restored from backups to the moment before it, check the rows, and point your app at it.

When a bad write destroys data in a hosted database, you get it back by declaring a second database that replays the first one's backups up to a moment you name. The original stays as it is, so a mistaken restore can't make things worse. At the end, your app reads the restored copy.

There is no restore button or command. Hosted Postgres archives every change continuously, and a restore is a `ManagedDatabase` whose spec names the source and the time. [Managed Postgres](https://docs.reliantlabs.io/hosting/managed-postgres.md#backups-and-point-in-time-restore) explains the backups behind it.

## Before you begin

* A Forge app on Reliant hosting with a managed database, such as the one from [Deploy a Go API with managed Postgres](https://docs.reliantlabs.io/guides/hosting/deploy-go-api-managed-postgres.md).
* The database belongs to an environment in your organization. A [claimable deploy](https://docs.reliantlabs.io/forge/deploy-without-an-account.md) has no backups.
* The moment you want back is less than 7 days old, and after the database's first base backup.
* The organization's `db:query` grant, to check the restored rows with `forge db query`.

**1. Pick the moment to restore to**

Find the last moment the data was good, in UTC. The `db query` audit records, your app's logs and the release history (`reliant forge env status prod --history`) help you date the incident. Confirm what the damage looks like:

```bash terminal theme={null}
reliant forge db query prod "select count(*) from orders" \
  --confirm-env prod --reason "INC-12: orders deleted"
```

```text Output theme={null}
count
0
(1 row, 14 ms; release v69.1.2; audit 53a12329-6ace-4d38-9a9e-7813670b1a70)
```

The table had three rows at `2026-11-20T15:02:09Z` and none at 15:06, so this guide restores to `2026-11-20T15:02:00Z`. Pick a time a little before the incident: recovery stops at the timestamp, and a change committed after it is not replayed.

**2. Declare the restored database**

Add a second `ManagedDatabase` to the environment's KCL, beside the one it restores, and list both in the bundle:

```kcl deploy/kcl/prod/main.k theme={null}
_db = forge.ManagedDatabase {
    name = "shop"
    runtime = forge.OnHosted {}
}

# shop as it was before the bad delete.
_restored = forge.ManagedDatabase {
    name = "shop-restored"
    runtime = forge.OnHosted {}
    spec = {
        restore = {
            sourceDatabase = "shop"
            pointInTime = "2026-11-20T15:02:00Z"
        }
    }
}

_bundle = forge.Bundle {
    # ...
    databases = [_db, _restored]
}
```

* `sourceDatabase` is required and must name a database in the same environment.
* `pointInTime` is an RFC 3339 timestamp. Leave it out to replay every archived change.
* The name follows the usual rule: a lowercase DNS label of at most 40 characters.

A restore applies only when the database is created. Adding `restore` to a database that already exists does nothing, which is why you restore into a new one.

**3. Check the render**

```bash terminal theme={null}
reliant forge env render prod --list
```

```text Output theme={null}
CLUSTER  KIND             NAMESPACE  NAME           APP
hosted   Workload         -          migrate        -
hosted   Workload         -          api            -
hosted   ManagedDatabase  -          shop           -
hosted   ManagedDatabase  -          shop-restored  -
```

**4. Deploy it**

Commit, cut a release and deploy it:

```bash terminal theme={null}
reliant forge env build prod --release v69.1.2
reliant forge env deploy prod v69.1.2 --yes
```

```text Output (trimmed) theme={null}
    first publish  shop-restored (database) — not on the control plane yet; this deploy creates it
  readiness: all 4 workload(s) ready
Rollout of v69.1.2 to prod: SUCCEEDED
  not pinned by this release (reported, does not gate):
    shop           ready
    shop-restored  ready
```

The platform creates `shop-restored` from the source's newest base backup before your timestamp, then replays the archive up to it. The app still reads `shop`.

**5. Check the restored rows**

Query the restored database by name:

```bash terminal theme={null}
reliant forge db query prod "select id, customer, total_cents from orders order by id" \
  --database shop-restored --confirm-env prod --reason "INC-12: check the restore"
```

You should see the rows as they were at `pointInTime`. Rows written after it are not there, by design.

**6. Point the app at the restored database**

The API and the migrate job read `DATABASE_URL` from the database the env passes to its hosted binder. Point it at the restored one:

```kcl deploy/kcl/prod/main.k theme={null}
_hosted = lambda w: fw.Workload -> fw.Workload {
    envbase.hosted(w, appcfg.app_config, _restored.name)
}
```

> [!WARNING]
>
> The restored database has the migration history of `pointInTime`. If the damage came from a migration, the migrate job applies it again on the next deploy. Replace that migration's SQL with a no-op (`SELECT 1;`, with a comment saying why) in the same commit.

Cut a release and deploy it. The migrate job brings the restored database's schema to head, then the API rolls onto it.

**7. Verify the app serves the restored data**

`readyz` checks the API's database connection, and any RPC that reads the table shows the data. Here, an RPC that lists order ids:

```bash terminal theme={null}
curl -s https://agile-kestrel.reliantlabs.dev/readyz
curl -s -X POST -H 'Content-Type: application/json' -d '{}' \
  https://agile-kestrel.reliantlabs.dev/services.catalog.v1.CatalogService/ListOrderIDs
```

```text Output theme={null}
{"status":"ready","checks":[{"name":"db","ok":true},{"name":"api_listener","ok":true}]}
{"ids":["ord-1001", "ord-1002", "ord-1003"]}
```

The three orders the cleanup deleted are back.

**8. Clean up**

Keep `_db` declared until you no longer need it: hosted databases default to `deletionPolicy = retain`, so removing a declaration does not drop its data. To keep data when you delete a whole environment, use `forge env delete <env> --keep-data` ([keeping data](https://docs.reliantlabs.io/hosting/managed-postgres.md#delete-an-environment-keep-the-data)).

## What this does not do

* There is no restore command or button. The declaration is the whole procedure, so it is reviewed, versioned and repeatable like any other change.
* It never restores over the source. The original keeps running, unchanged, and you decide when to stop using it.
* It can't cross environments. You can't restore prod's backups into staging.
* It can't reach back more than 7 days, or before the database's first base backup.
* It needs the platform's backup storage to be ready for the database. Until it is, the database has no backups and nothing to restore from. No forge command prints a database's backup status yet. Reliant's own monitoring pages the platform's on-call when a database's backups are on and failing.

## Troubleshooting

> [accordion] restore.pointInTime "…" must be an RFC3339 timestamp (2026-01-02T15:04:05Z)
>
> Write the time in full UTC with a `Z`, or with an explicit offset: `2026-11-20T15:02:00Z`.

> [accordion] spec.restore needs an object store, and this environment declares none
>
> The environment has no backup storage, so there are no backups to read. Only databases in an account's environments are backed up.

> [accordion] attribute 'DatabaseRestore' not found in 'module 'forge''
>
> Write `restore` as a plain block inside `spec`, as in this guide, not as `forge.DatabaseRestore { … }`.

> [accordion] the database is unreachable: connect to … failed
>
> `forge db query` could not connect to the restored database. The exit code is 2: forge could not look, which says nothing about the rows. Check that the deploy reported the database `ready`, then retry, or check the data through your app once it reads the restored database.

## Next steps

> [card] [Managed Postgres](https://docs.reliantlabs.io/hosting/managed-postgres.md)
>
> Backups, the read-only query path and keeping data when you delete an environment.

> [card] [Zero-downtime migrations](https://docs.reliantlabs.io/guides/forge/zero-downtime-postgres-migrations.md)
>
> Catch a destructive migration before it ships.

> [card] [Let agents query production data](https://docs.reliantlabs.io/guides/forge/let-agents-query-production-data.md)
>
> Audited, read-only queries for people and agents.

> [card] [Safe migrations](https://docs.reliantlabs.io/deep-dives/safe-migrations.md)
>
> How forge keeps a migration from wedging or breaking a deploy.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.