|
| 1 | +--- |
| 2 | +title: COPY FROM / COPY TO |
| 3 | +description: Bulk import and export of collections using NDJSON, JSON array, and CSV formats. |
| 4 | +--- |
| 5 | + |
| 6 | +# COPY FROM / COPY TO |
| 7 | + |
| 8 | +`COPY` moves data between a NodeDB collection and a file on the server's filesystem. It is the fastest way to load or dump large datasets. |
| 9 | + |
| 10 | +## COPY FROM — import |
| 11 | + |
| 12 | +```sql |
| 13 | +COPY <collection> FROM '<path>' [WITH (FORMAT <fmt> [, DELIMITER '<char>'] [, HEADER <bool>])] |
| 14 | +``` |
| 15 | + |
| 16 | +Reads the file at `<path>` on the server and inserts every record into `<collection>`. The format is either specified explicitly or auto-detected from the file extension. |
| 17 | + |
| 18 | +### Auto-detected formats |
| 19 | + |
| 20 | +| Extension | Format | |
| 21 | +| ------------------- | ------------ | |
| 22 | +| `.ndjson`, `.jsonl` | NDJSON | |
| 23 | +| `.json` | JSON array | |
| 24 | +| `.csv` | CSV | |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +```sql |
| 29 | +-- NDJSON (one JSON object per line) — format auto-detected from extension |
| 30 | +COPY users FROM '/data/users.ndjson'; |
| 31 | + |
| 32 | +-- JSON array (a single top-level array of objects) |
| 33 | +COPY users FROM '/data/users.json' WITH (FORMAT json_array); |
| 34 | + |
| 35 | +-- CSV with header row (default when FORMAT csv) |
| 36 | +COPY orders FROM '/data/orders.csv' WITH (FORMAT csv); |
| 37 | + |
| 38 | +-- CSV without header row |
| 39 | +COPY orders FROM '/data/orders.csv' WITH (FORMAT csv, HEADER false); |
| 40 | + |
| 41 | +-- CSV with non-comma delimiter |
| 42 | +COPY products FROM '/data/products.tsv' WITH (FORMAT csv, DELIMITER '\t'); |
| 43 | +``` |
| 44 | + |
| 45 | +### WITH options |
| 46 | + |
| 47 | +| Option | Values | Default | Description | |
| 48 | +| ----------- | ------------------------------- | ----------------- | --------------------------------------- | |
| 49 | +| `FORMAT` | `ndjson`, `json_array`, `csv` | auto from extension | File format | |
| 50 | +| `DELIMITER` | Any single character | `,` | Field separator (CSV only) | |
| 51 | +| `HEADER` | `true` / `false` | `true` | Whether the first CSV row is a header | |
| 52 | + |
| 53 | +## COPY TO — export |
| 54 | + |
| 55 | +```sql |
| 56 | +COPY <collection> TO '<path>' [WITH (FORMAT <fmt> [, DELIMITER '<char>'] [, HEADER <bool>])] |
| 57 | +COPY (SELECT ...) TO '<path>' [WITH (...)] |
| 58 | +``` |
| 59 | + |
| 60 | +Writes every row from `<collection>` (or the result of a SELECT) to a file at `<path>` on the server. The format is auto-detected from the extension or specified explicitly. |
| 61 | + |
| 62 | +### Examples |
| 63 | + |
| 64 | +```sql |
| 65 | +-- Export entire collection as NDJSON |
| 66 | +COPY users TO '/exports/users.ndjson'; |
| 67 | + |
| 68 | +-- Export with explicit format |
| 69 | +COPY users TO '/exports/users.json' WITH (FORMAT json_array); |
| 70 | + |
| 71 | +-- Export as CSV with header |
| 72 | +COPY orders TO '/exports/orders.csv' WITH (FORMAT csv); |
| 73 | + |
| 74 | +-- Export the result of a query |
| 75 | +COPY (SELECT id, name, email FROM users WHERE status = 'active') |
| 76 | + TO '/exports/active_users.csv' WITH (FORMAT csv); |
| 77 | + |
| 78 | +-- Query export as NDJSON |
| 79 | +COPY (SELECT * FROM orders WHERE created_at > '2025-01-01') |
| 80 | + TO '/exports/recent_orders.ndjson'; |
| 81 | +``` |
| 82 | + |
| 83 | +## Notes |
| 84 | + |
| 85 | +- Paths are resolved on the **server** filesystem. The client never streams file bytes — this is not `\copy` (psql client-side copy). |
| 86 | +- `COPY FROM STDIN` is reserved for the backup/restore path and is not available as a general import mechanism. |
| 87 | +- Rows that fail type coercion during import are rejected with a parse error; no partial-row insertion occurs. |
| 88 | +- `COPY TO` is non-transactional: the file is written even if the session is later rolled back. |
| 89 | +- The `COPY (SELECT ...) TO` form supports all SELECT features including JOINs, CTEs, and WHERE clauses. |
0 commit comments