COPY ... FROM statement copies data from or other to tables in your cluster. The COPY ... TO statement allows you to export a table or arbitrary query in a text or CSV format.
Syntax
Parameters
| Parameter | Description |
|---|---|
table_name | The name of the table to which to copy data. |
opt_column_list | The column name, or list of column names, to which to copy data. |
WITH copy_options | Optionally specify one or more copy options. |
query | A , , , , or statement for which to copy results. |
Options
| Option | Description |
|---|---|
DELIMITER 'value' | The value that delimits the rows of input data, passed as a string. |
NULL 'value' | The string that represents a NULL value in the input data. |
BINARY | Copy data FROM binary format. If BINARY is specified, no other format can be specified.If no format is specified, CockroachDB copies in plaintext format. |
CSV | Copy data FROM CSV format, or copy data TO CSV format. If CSV is specified, no other format can be specified.If no format is specified, CockroachDB copies in plaintext format. |
ESCAPE | Specify an escape character for quoting the fields in CSV data. |
HEADER | Specify that CockroachDB should skip the header in CSV data (first line of input). |
Required privileges
Only members of theadmin role can run COPY statements. By default, the root user belongs to the admin role.
Unsupported syntax
CockroachDB does not yet support the followingCOPY syntax:
COPY ... WITH FREEZE.COPY ... WITH QUOTE.COPY ... FROM ... WHERE <expr>.
Examples
To run the examples, use to start a temporary, in-memory cluster with the preloaded.Copy tab-delimited data to CockroachDB
-
Start copying data to the
userstable: -
You will see the following prompt:
- Enter some tab-delimited data to copy to the table:
Before you input the following rows, ensure the delimiters are tab characters. They may have been converted to spaces by the browser.
-
Mark the end of data with
\.on its own line: -
Query the
userstable for the rows that you just inserted:
Copy CSV-delimited data to CockroachDB
You can copy CSV data into CockroachDB using the following methods:- Copy CSV-delimited data from
stdin - Copy CSV-delimited data from
stdinwith an escape character - Copy CSV-delimited data from
stdinwith a header - Copy CSV-delimited data from
stdinwith hex-encoded byte array data
Copy CSV-delimited data from stdin
-
Create a new table that you will load with CSV-formatted data:
-
Start copying data to the
setecastronomytable:You will see the following prompt: -
Enter some CSV-delimited data to copy to the table:
-
Mark the end of data with
\.on its own line: -
View the data in the
setecastronomytable:
Copy CSV-delimited data from stdin with an escape character
-
Create a new table that you will load with CSV-formatted data:
-
Start copying data to the
setecastronomytable, specifying an escape character for quoting the fields:You will see the following prompt: -
Enter some CSV-delimited data to copy to the table:
-
Mark the end of data with
\.on its own line: -
View the data in the
setecastronomytable:
Copy CSV-delimited data from stdin with a header
-
Create a new table that you will load with CSV-formatted data:
-
Start copying data to the
setecastronomytable, specifying that CockroachDB should skip the header (first line of CSV input): -
Enter the data, including the header line:
-
Mark the end of data with
\.on its own line: -
View the data in the
setecastronomytable:
Copy CSV-delimited data from stdin with hex-encoded byte array data
-
Create a new table that you will load with CSV-formatted data:
-
Set the
bytea_outputto specify that CockroachDB should ingest hex-encoded byte array data: -
Start copying data to the
mybytestable: -
Enter some CSV-delimited data to copy to the table:
-
Mark the end of data with
\.on its own line: -
View the data in the
mybytestable:
Copy data to stdout in CSV format
- Start a temporary, in-memory cluster with the sample dataset preloaded:
-
Copy five rows from the
userstable tostdout, specifying theCSVoption:

