import and export move data between DuckDB tables and columnar files. import creates a table
from a data file (or glob); export copies a query’s result to a file. Both are gated by the
path scope and allow_path policy because they touch the filesystem.
DataFormat
Both commands take a format of'parquet' | 'csv' | 'json':
import
Createtable from a data file by reading it through DuckDB’s auto-reader for the format.
-
Signature:
import(table: string, source: string, format: DataFormat): Promise<ExecuteResult>. -
sourcemay be a path or a glob. The backend runsCREATE TABLE <table> AS SELECT * FROM <reader>(<source>), where the reader is: -
ExecuteResult.rowsAffectedis the number of rows inserted.
Typed helpers
import with the format fixed.
export
Copy a query’s result to a file.- Signature:
export(query: string, path: string, format: DataFormat): Promise<ExecuteResult>. - The backend runs
COPY (<query>) TO '<path>' (FORMAT <PARQUET|CSV|JSON>).
Typed helpers
exportJson helper; call export(query, path, 'json') directly.)
Safety and gotchas
The
table name is validated as a bare SQL identifier (ASCII alphanumeric + _); anything
else is rejected with backend error: invalid table identifier: …. The source/path strings
are quoted as SQL literals, and the path is checked against the path scope and
allow_path policy.importis a write (CREATE TABLE), so it is rejected underread_only(true).exportis a read of the source table but still writes a file, so it is not blocked byread_only— it is gated only by the path checks and the statement filter.- Path scope applies to the file argument:
import’ssourceandexport’spath. If a capability declarespathallow entries, the value must match a whole-component prefix (and..traversal is rejected). Theallow_pathpolicy applies the same constraint at init time. See Scope & Policy. - Relative paths resolve against the app’s working directory. In the
example app the capability scopes both to
data/.
See also
- Append rows — insert in-memory rows without a file.
- Scope & Policy — the path allow-list that gates both commands.
- Arrow & Polars — the columnar interchange underneath.