
# Importing existing data

`agc import <dump>` checks a dump's format, size and plain-SQL ownership statements on your machine, and returns the console import link with your migration files in it. Nothing is uploaded from the CLI; a person uploads from that link. Keep source writes running until the import has finished.

## Ask your agent

```text
Check my Postgres dump with agc import and give me the console link. Keep source writes and traffic in place until the import has finished.
```

The agent reports validation results and the link. A person opens the link, uploads and confirms.

```text
Prepare a production migration plan and validate the export locally. Do not claim the migration is complete until the import has finished.
```

The agent reports local validation and the next step. A person keeps control of the source database and any release approval.

## Steps

Make the dump. Custom format is the best choice: the restore leaves out owners and privileges itself.

```bash
pg_dump --format=custom "$SOURCE_DATABASE_URL" > camp.pgdump
```

A plain dump must leave them out:

```bash
pg_dump --format=plain --no-owner --no-privileges "$SOURCE_DATABASE_URL" > camp.sql
```

Check it and get the link:

```bash
agc import camp.pgdump
```

The CLI returns `data.import_url`, the console import link for the app, with your migration files in its fragment. A person opens it to upload and confirm (an agent never imports).

## How it works

**What agc checks, locally.** Nothing is uploaded. agc checks:

- the format, by its first bytes: custom starts with `PGDMP`, tar has `ustar` at byte 257, a folder with `toc.dat` is a directory dump, and anything else text is plain SQL;
- the size (a directory dump counts all its files);
- for plain SQL, that no `ALTER … OWNER TO`, `GRANT` or `REVOKE` statement appears outside strings, comments and `COPY` rows.

**Backend.** The import API and restore worker support multipart uploads, an atomic restore, and recording existing migration checksums as applied. Imports are accepted only before the first release, and only from a person.

## Migrating with minimal downtime

Keep production on the source system while you prepare the app and the dump. The link `agc import` returns is not proof that data reached Agent Cloud; the import's op reaching `imported` is.

1. Prepare the app on its mirror, including plain SQL migrations and representative seed data.
2. Have the person export their source database using its documented connection path. Keep credentials out of chat.
3. Validate the dump locally with `agc import dump.pgdump`.
4. Have the person open the link, upload and confirm; follow it with `agc status` and `agc wait <op>`.
5. Freeze writes at the source only for the final export, then switch DNS and release.

The backend restores in a transaction and records migration files as applied, before the first release only. Freeze source writes for the final export so the dump is consistent. For a release, success is exit 0 with `"outcome": "live"`, and a receipt or pending approval doesn't establish it.

## Reference

| Item | Value |
|---|---|
| Formats | custom (`-Fc`), tar, directory, plain SQL with `COPY` rows |
| Plain dump encoding | UTF-8 only |
| Cancel an unfinished upload | `agc import --cancel` |
| Op states | `uploading`, `restoring`, `imported`, `failed`, `cancelled`, `expired`, `held` |

## Errors

| `code` | Exit | Cause | Fix |
|---|---|---|---|
| `import.no_file` | 2 | No path given | `agc import dump.pgdump` |
| `import.not_found` | 2 | No file at that path | Check the path |
| `import.not_a_dump` | 2 | Binary but not custom or tar, or a folder without `toc.dat` | Pass a file pg_dump wrote |
| `import.too_large` | 2 | Over 5 GB | Prepare smaller dumps by table (`pg_dump --format=custom --table=<name>`) |
| `import.plain_has_owners` | 2 | A plain dump sets owners or privileges | Dump with `--no-owner --no-privileges`, or use custom format |
| `import.too_many_migrations` | 2 | More than 300 migration files | Squash them before the first ship |
| `import.after_ship` | 1 | The app already has a release | Use a migration |
| `import.nothing_to_cancel` | 1 | `--cancel` with no upload in progress | A restore that started runs to its end |
| `import.running`, `import.busy` | page | Another import, or a ship, is in progress or waiting | Wait for it (`agc wait <op>`) |
| `import.restore_failed` | op | The restore stopped; production is as it was | Fix the object it names, import again |
| `import.extension_unavailable` | op | The dump needs an extension production does not offer | Dump without what uses it |
| `import.bad_encoding`, `import.line_too_long` | op | Plain dump not UTF-8, or a line over 256 MB | Use custom format |
| `ship.secret_missing` | 1 | A required secret isn't set | Set it before any write freeze |
| `domain.unclaimed` | 1 | The app has no organization | Ship the first release first |

```json
{
  "ok": false,
  "product": "agent.cloud",
  "command": "import",
  "error": {
    "code": "import.plain_has_owners",
    "message": "camp.sql sets owners or privileges (ALTER TABLE, line 41), for roles production doesn't have.",
    "fix": "Make the dump without them: `pg_dump --format=plain --no-owner --no-privileges <database> > dump.sql`, or use the custom format (`pg_dump --format=custom <database> > dump.pgdump`), whose ownership and privileges the restore leaves out itself.",
    "retryable": false,
    "docs": "agc help import"
  },
  "next": [{ "cmd": "agc help import", "why": "what agc import takes" }]
}
```

```json
{
  "ok": false, "product": "agent.cloud", "command": "import",
  "error": { "retryable": false, "code": "import.after_ship",
             "message": "camp-bookings has shipped, so production is the app’s own now.",
             "fix": "Change production with a migration instead: an import only loads an app before its first ship." },
  "next": [ { "cmd": "agc help migrations", "why": "how migrations change production" } ]
}
```

## Limits

| What | Value |
|---|---|
| Dump size | 5 GB per import |
| Migration files recorded | 300 |
| Upload part | 8 MB |
| Idle upload expiry | 24 hours |
| Failed upload kept | 7 days |
| Imports at a time per app | 1 |
| Console production login | 1 hour, read-only |

## Agent notes

- `agc import` only checks the file and returns the link for the person; the person uploads. The URL is not evidence the data arrived.
- Run it from the app folder with the final `migrations/`: the link records exactly the files present then, and a resumed upload refuses a different list (`import.baseline_changed`).

## Related

- [Migrations](/docs/data/migrations.md)
- [Your app's Postgres](/docs/data/your-apps-postgres.md)
- [Data on private targets](/docs/data/reading-production-safely.md#data-on-private-targets)
- [Quickstart: an existing app](/docs/getting-started/quickstart-existing-app.md)
- [Migrate from Vercel](/docs/compare/agent-cloud-vs-vercel.md#moving-from-vercel)
- [Migrate from Railway / Render / Heroku](/docs/compare/agent-cloud-vs-railway.md#moving-from-railway-render-fly-or-heroku)
- [Migrate from Lovable / Bolt / Replit](/docs/compare/agent-cloud-vs-lovable-bolt-replit.md#moving-from-lovable-bolt-or-replit)
- [Custom domains](/docs/build/custom-domains.md)

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