Skip to main content
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

copy syntax diagram

Parameters

ParameterDescription
table_nameThe name of the table to which to copy data.
opt_column_listThe column name, or list of column names, to which to copy data.
WITH copy_optionsOptionally specify one or more copy options.
queryA , , , , or statement for which to copy results.

Options

OptionDescription
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.
BINARYCopy data FROM binary format. If BINARY is specified, no other format can be specified.
If no format is specified, CockroachDB copies in plaintext format.
CSVCopy 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.
ESCAPESpecify an escape character for quoting the fields in CSV data.
HEADERSpecify 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

  1. Start copying data to the users table:
  2. You will see the following prompt:
  3. 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.
  1. Mark the end of data with \. on its own line:
  2. 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

  1. Create a new table that you will load with CSV-formatted data:
  2. Start copying data to the setecastronomy table:
    You will see the following prompt:
  3. Enter some CSV-delimited data to copy to the table:
  4. Mark the end of data with \. on its own line:
  5. View the data in the setecastronomy table:

Copy CSV-delimited data from stdin with an escape character

  1. Create a new table that you will load with CSV-formatted data:
  2. Start copying data to the setecastronomy table, specifying an escape character for quoting the fields:
    You will see the following prompt:
  3. Enter some CSV-delimited data to copy to the table:
  4. Mark the end of data with \. on its own line:
  5. View the data in the setecastronomy table:

Copy CSV-delimited data from stdin with a header

  1. Create a new table that you will load with CSV-formatted data:
  2. Start copying data to the setecastronomy table, specifying that CockroachDB should skip the header (first line of CSV input):
  3. Enter the data, including the header line:
  4. Mark the end of data with \. on its own line:
  5. View the data in the setecastronomy table:

Copy CSV-delimited data from stdin with hex-encoded byte array data

  1. Create a new table that you will load with CSV-formatted data:
  2. Set the bytea_output to specify that CockroachDB should ingest hex-encoded byte array data:
  3. Start copying data to the mybytes table:
  4. Enter some CSV-delimited data to copy to the table:
  5. Mark the end of data with \. on its own line:
  6. View the data in the mybytes table:

Copy data to stdout in CSV format

  1. Start a temporary, in-memory cluster with the sample dataset preloaded:
  2. Copy five rows from the users table to stdout, specifying the CSV option:

See also