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
Options
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:

