
# Reading production safely

`agc db query "<sql>" --env prod` runs one read-only statement against a masked branch of production, never production itself. It returns at most 100 rows, and `data.as_of` says when the branch was taken. Success is `"ok": true` with `"env": "prod"` and `"read_only": true`.

```bash
agc db query "SELECT status, count(*) FROM bookings GROUP BY 1" --env prod
```

```json
{
  "ok": true,
  "product": "agent.cloud",
  "command": "db query",
  "data": {
    "env": "prod",
    "read_only": true,
    "rows": [{ "status": "confirmed", "count": "412" }, { "status": "cancelled", "count": "37" }],
    "row_count": 2,
    "truncated": false,
    "masked_columns": [],
    "as_of": "2026-10-20T14:52:03.118Z"
  },
  "next": []
}
```

## Ask your agent

```text
How many bookings were cancelled in production this week? Use Agent Cloud's masked production read and tell me only the count.
```

## How it works

**A masked branch, not production.** Agent Cloud keeps one masked branch of production per app for these reads. It's masked with the same policy as every mirror: names and free text replaced, numbers, dates and labels kept, JSON blanked ([Masking](/docs/concepts/masking.md) has every rule). This branch follows the platform policy and the owner's choices; the manifest's `mask` rules apply to mirrors from `agc up`.

**How fresh.** Use `--fresh` for data written moments ago. agc waits up to 300 seconds for a new branch, then answers `control.timeout`. Run `agc status`, then the query again.

| Current branch | The query |
|---|---|
| Under 10 minutes old | Answers from it |
| 10 minutes to an hour old | Answers from it with `"refreshing": true` while a new one is built |
| Over an hour old, or older than the last release (its migrations may have changed the schema) | Waits for a new branch |
| Any age, with `--fresh` | Waits for a branch taken now, a minute or two |

**Read-only by construction.** It runs as one prepared statement in a session whose transactions are all read-only, with a 5-second statement timeout. Past that it fails with `query.timeout` (HTTP 504), so aggregate or filter by indexed columns. If the masked copy can't be reached, it's `query.unreachable` (503). Both are safe to rerun. A second statement is an error.

**Rows.** At most 100 come back. `data.row_count` counts them before the cap, and `data.truncated` says there were more.

**Tables masking emptied.** If masking had to empty a table on the branch (a masked value would break one of its keys), it reads 0 rows. `data.emptied` names each such table and why (`table`, `reason`). A table that references an emptied one is named with that as the reason.

**Without `--env prod`**, `agc db query` runs any SQL on your mirror ([Mirror DB tools](/docs/data/mirror-db-tools.md)).

## Console database access

The console's **Database** page (`/console/apps/<app>/database`) shows production's tables and what mirrors mask. There the app's owner, or an owner of its organization, can make a 60-minute read-only Postgres login. No `/v1` route makes one, so no agent or deploy key can.

1. Open the app in the console and go to **Database**.
2. Press **Connect read-only** and choose the kind (table below). For production, type the app's name to confirm.
3. Press **Create a login for 1 hour** and copy the URI under **Your login**. It's shown once, not again on reload.
4. Connect with any Postgres client: `psql "<the URI from the page>"`.
5. Under **Active logins**, press **Revoke now** to end one early.

| Choice | The page says |
|---|---|
| **Masked branch of production** | "The data agents see, on a branch made for this login. Safe to share." |
| **Production, real data** | "Only you. Don’t give this to an agent: agents get a masked branch. Each session is recorded." |

**The tables view.** Each table shows rows, size, and how many columns mirrors mask; each column gets a chip: `Masked · <type>`, `Visible`, or `agent.cloud’s ledger` for `agentcloud_migrations`. Which columns show as they are is the owner's choice on the app's Settings page ([Masking overrides](/docs/concepts/masking.md#making-a-column-visible)).

**The login.** Each login is its own Postgres role, `agc_ro_<id>` (settings below). A masked login gets its own masked branch, deleted with the login. A production login runs on production itself and needs a console sign-in within the last 12 hours.

| Setting or event | Value |
|---|---|
| Access | `pg_read_all_data`, `default_transaction_read_only = on` |
| Lifetime | 60 minutes; the reaper ends it and its sessions then, or on revoke |
| Connections | At most 2 at once for that role |
| Timeouts | 30 s per statement, 60 s idle in transaction |
| TLS | `sslmode=require` in the URI |
| Events | `db.credential.issued` (made), `db.credential.session` (a session on a production login), `db.credential.revoked`, `db.credential.expired` (ended at its hour) |

Errors show above the tables, each with its fix: two active logins per kind, ten logins a day, a typed app name for production, and a console sign-in within 12 hours.

## Data on private targets

When an organization runs its apps on its own target (a customer's AWS account, `operator = 'customer'`), production data never reaches an agent, not even masked, unless an owner allows production reads. `agc status` shows the target with `mirrors: empty`. Your mirror starts with production's schema and migration ledger and no rows, then gets your [seed data](/docs/data/mirror-db-tools.md#seed-data):

```toml
[data.db]
seed = "seed.sql"
```

```bash runnable
agc status
agc up --detach
agc check
```

| Path | On a customer's target | On Agent Cloud's cloud or a platform target |
|---|---|---|
| `agc up` | Schema-only branch with production's ledger copied in; migrations, then the seed | Masked branch of production |
| `agc db reset` | Back to schema, migrations and seed | A fresh masked branch |
| `agc up --at <time>` | Refused, `target.private_data` | A masked copy from the past |
| `agc db query --env prod` | Refused, `target.private_data`, unless an owner allows production reads (below) | Masked read |
| `agc logs` | Refused, `target.private_data` | Scrubbed lines |
| `agc ship` rehearsal | Runs on a branch of production; results are pass or fail | Same |

**The mirror.** The copied ledger tells `agc migrate` what's applied; the mirror is empty, with nothing to mask.
**What this means for your work.** Seed the cases that matter (an empty list, a full camp, a cancelled booking), and lean on invariants and smoke flows: rehearsals still run them on real production, reporting only pass or fail.

**Letting agents read production on your own target.** An organization owner can turn on "Agents may read this organization’s apps’ production databases" in the organization's General settings (off by default). From the next query, `agc db query --env prod` from a connected Builder or Read-only agent runs on production itself, read-only and unmasked, through the target's runner. The whole result comes back, up to 32 MiB as JSON (`query.result_too_large` past that), with `data.as_of` set to when it ran. Other agents, deploy keys and task tokens still get `target.private_data`.

### How do I query a Flight SQL connection's production data?

With production reads allowed, add `--connection <slug>` to read a granted [Flight SQL connection](/docs/owners/connections.md) as its production read role, from inside the target, so no VPN:

```bash
agc db query --env prod --connection trades "SELECT count(*) FROM fills"
```

No row cap; past 5 minutes it's `control.timeout`, safe to rerun. Each refusal (`connection.not_granted`, `query.unsupported_type` and the rest) names its fix. When the lake refuses a statement (`query.failed`), you get the lake's own reason, then the statement, then a fix that fits it.

A platform target (one Agent Cloud operates) gets the right-hand column above.

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). Here, `target.private_data` (exit 12) is policy: report it and continue on the mirror.

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "db query",
  "error": {
    "code": "target.private_data",
    "message": "This app runs on a private target: production data never reaches an agent, not even masked.",
    "fix": "Query your mirror instead (`agc db query` without --env); ask the organization for anything only production knows.",
    "retryable": false,
    "docs": "agc help data"
  },
  "next": [{ "cmd": "agc up --detach", "why": "your mirror, from the seed" }]
}
```

## Limits

One statement, 100 rows, 5 s ([Limits and quotas](/docs/reference/limits-and-quotas.md)).

## Agent notes

- Keep data out of chat and commits: report counts and totals, not rows, even masked ones.
- A person who needs real values uses a [console login](#console-database-access); production changes go through migrations and `agc ship`.
- Never ask a person for a production login, its URI or production rows, even to get around `target.private_data`.

## Related

- [Masking](/docs/concepts/masking.md)
- [Mirror DB tools](/docs/data/mirror-db-tools.md)
- [Your app's Postgres](/docs/data/your-apps-postgres.md)
- [Where apps run](/docs/concepts/where-apps-run.md)
- [Data residency guarantees](/docs/security/production-data-protection.md#data-residency-on-private-targets)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "data/reading-production-safely: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
