The 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 the admin role can run COPY statements. By default, the root user belongs to the admin role.
Unsupported syntax
CockroachDB does not yet support the following COPY 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
users table:
-
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
users table 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
-
Create a new table that you will load with CSV-formatted data:
-
Start copying data to the
setecastronomy table:
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
setecastronomy table:
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
setecastronomy table, 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
setecastronomy table:
-
Create a new table that you will load with CSV-formatted data:
-
Start copying data to the
setecastronomy table, 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
setecastronomy table:
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_output to specify that CockroachDB should ingest hex-encoded byte array data:
-
Start copying data to the
mybytes table:
-
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
mybytes table:
-
Start a temporary, in-memory cluster with the sample dataset preloaded:
-
Copy five rows from the
users table to stdout, specifying the CSV option:
See also