The statements on this page are deprecated as of v23.1 and will be removed in a future release. To move data into CockroachDB, use or .
IMPORT imports the following types of data into CockroachDB:
To import CSV, Avro, or delimited data files, see .
Certain
IMPORT TABLE statements that defined the table schema inline are not supported in v22.1 and later versions. These include running IMPORT TABLE ... CREATE USING and IMPORT TABLE with any non-bundle format (CSV, DELIMITED, PGCOPY, or AVRO) data types.To import data into a new table, use followed by .IMPORT INTO supports CSV/TSV, Avro, and delimited data files. For an example, read .Considerations
IMPORTis a blocking statement. To run an import job asynchronously, use theDETACHEDoption.IMPORTcannot be used within a .- Certain
IMPORT TABLEstatements that defined the table schema inline are not supported in v22.1 and later versions. These include runningIMPORT TABLE ... CREATE USINGandIMPORT TABLEwith any non-bundle format (CSV,DELIMITED,PGCOPY, orAVRO) data types. Instead, useCREATE TABLEandIMPORT INTO; see this for more detail. - For instructions and working examples on how to migrate data from other databases, see the .
IMPORTcannot directly import data toREGIONAL BY ROWtables that are part of . Instead, use which supports importing intoREGIONAL BY ROWtables.
Required privileges
Table privileges
The user must have theCREATE on the target database.
Source privileges
You can grant a user theEXTERNALIOIMPLICITACCESS to interact with external resources that require implicit access.
Either the EXTERNALIOIMPLICITACCESS or the role is required for the following scenarios:
- Interacting with a cloud storage resource using .
- Using a custom endpoint on S3.
- Using the command.
- Using or HTTPS.
- Interacting with an Amazon S3 and Google Cloud Storage resource using
SPECIFIEDcredentials. Azure Storage is alwaysSPECIFIEDby default. - Using storage.
While Cockroach Labs actively tests Amazon S3, Google Cloud Storage, and Azure Storage, we do not test S3-compatible services (e.g., MinIO, Red Hat Ceph).
Synopsis
Parameters
For import from dump file
| Parameter | Description |
|---|---|
table_name | The name of the table you want to import/create. Use this when the dump file contains a specific table. Leave out TABLE table_name FROM when the dump file contains an entire database. |
import_format | PGDUMP OR MYSQLDUMP |
file_location | The URL of a dump file you want to import. |
WITH kv_option_list | Control your import’s behavior with these options. |
Import options
You can control theIMPORT process’s behavior using any of the following optional key-value pairs as a kv_option. To set multiple import options, use a comma-separated list (see examples).
Requirements
Before you begin
Before usingIMPORT, you should have:
- The schema of the table you want to import.
- The data you want to import, preferably hosted on cloud storage. This location must be equally accessible to all nodes using the same import file location. This is necessary because the
IMPORTstatement is issued once by the client, but is executed concurrently across all nodes of the cluster. For more information, see Import file location.
IMPORT, see Considerations.
Import targets
Imported tables must not exist and must be created in theIMPORT statement with the schema and data importing from the same source. If the table you want to import already exists, you must drop it with or use .
You can specify the target database in the table name in the IMPORT statement. If it’s not specified there, the active database in the SQL session is used.
Create table
YourIMPORT statement must reference an import file that specifies the schema of the data you want to import. You have several options:
-
Load a file that already contains a
CREATE TABLEstatement. For an example, see Import a PostgreSQL database dump below. - Use followed by . For an example, see .
IMPORTsupports for PostgreSQL dump files only.- By default, the PostgreSQL and MySQL import formats support foreign keys. However, the most common dependency issues during import are caused by unsatisfied foreign key relationships that cause errors like
pq: there is no unique constraint matching given keys for referenced table tablename. You can avoid these issues by adding theskip_foreign_keysoption to yourIMPORTstatement as needed. Ignoring foreign constraints will also speed up data import.
Available storage
Each node in the cluster is assigned an equal part of the imported data, and so must have enough temp space to store it. In addition, data is persisted as a normal table, and so there must also be enough space to hold the final, replicated data. The node’s first-listed/default directory must have enough available storage to hold its portion of the data. On , if you set--max-disk-temp-storage, it must also be greater than the portion of the data a node will store in temp space.
Import file location
CockroachDB uses the URL provided to construct a secure API call to the service you specify. The URL structure depends on the type of file storage you are using. For more information, see the following:Table users and privileges
Imported tables are treated as new tables, so you must privileges to them.Performance
- All nodes are used during the import job, which means all nodes’ CPU and RAM will be partially consumed by the
IMPORTtask in addition to serving normal traffic. - To improve performance, import at least as many files as you have nodes (i.e., there is at least one file for each node to import) to increase parallelism.
- To further improve performance, order the data in the imported files by and ensure the primary keys do not overlap between files.
- An import job will pause if a node in the cluster runs out of disk space. See Viewing and controlling import jobs for information on resuming and showing the progress of import jobs. For instructions on how to free up disk space as quickly as possible after dropping a table, see
- An import job will instead of entering a
failedstate if it continues to encounter transient errors once it has retried a maximum number of times. Once the import has paused, you can either or it.
Viewing and controlling import jobs
After CockroachDB initiates an import, you can view its progress with and on the of the DB Console, and you can control it with , , and .If initiated correctly, the statement returns when the import is finished or if it encounters an error. In some cases, the import can continue after an error has been returned (the error message will tell you that the import has resumed in the background).When , imports now continue from their internally recorded progress instead of starting over.
Examples
The following examples make use of:- Amazon S3 connection strings. For guidance on connecting to other storage options or using other authentication parameters instead, read .
- The default
AUTH=specifiedparameter. For guidance on usingAUTH=implicitauthentication with Amazon S3 buckets instead, read .
- External connections, which allow you to represent an external storage or sink URI. You can then specify the external connection’s name in statements rather than the provider-specific URI. For detail on using external connections, see the page.
- Assume role authentication, which allows you to limit the control specific users have over your storage buckets. See for more information.
Import a PostgreSQL database dump
pg_dump, and use the WITH ignore_unsupported_statements clause. For more information, see .
Import a table from a PostgreSQL database dump
WITH skip_foreign_keys option may be needed. For more information, see the list of import options.
For this command to succeed, you need to have created the dump file with specific flags to pg_dump. For more information, see .
Import a MySQL database dump
Import a table from a MySQL database dump
WITH skip_foreign_keys option may be needed. For more information, see the list of import options.
For more detailed information about importing data from MySQL, see .
Import a limited number of rows
Therow_limit option determines the number of rows to import. This option will import the first n rows from each table in the dump file. It is useful for finding errors quickly before executing a more time- and resource-consuming import. Imported tables can be inspected for their schema and data, but must be before running the actual import.
Import a compressed file
CockroachDB chooses the decompression codec based on the filename (the common extensions.gz or .bz2 and .bz) and uses the codec to decompress the file during import.
decompress option to specify the codec to be used for decompressing the file during import:
Run an import within a transaction
TheDETACHED option allows an import to be run asynchronously, returning the job ID immediately once initiated. You can run imports within transactions by specifying the DETACHED option.
To use the DETACHED option with IMPORT in a transaction:
DETACHED option, IMPORT will block the SQL connection until the job completes. Once finished, the job status and more detailed job data is returned:
Import a table from a local file
You can import a file fromnodelocal, which is the external IO directory on a node’s local file system. To import from nodelocal, a nodeID is required and the data files will be in the extern directory of the specified node.
The file system backup location on the NFS drive is relative to the path specified by the
--external-io-dir flag set while . If the flag is set to disabled, then imports from local directories and NFS drives are disabled. Use self if you do not want to specify a nodeID, and the individual data files will be in the extern directories of arbitrary nodes; however, to work correctly, each node must have the point to the same NFS mount or other network-backed, shared storage.nodeID is provided, the data files to import will be in the extern directory of the specified node:
nodeID in the IMPORT statement:
Import data into your CockroachDB Cloud cluster
You can import data into your CockroachDB Cloud cluster using either or :Import using userfile
To import from userfile, first create the table that you would like to import into:
IMPORT INTO to import data into the table:
userfile:/// references the default path (userfile://defaultdb.public.userfiles_$user/).
Import using cloud storage
To import a table into your cluster:Known limitation
can sometimes fail with a “context canceled” error, or can restart itself many times without ever finishing. If this is happening, it is likely due to a high amount of disk contention. This can be mitigated by setting thekv.bulk_io_write.max_rate to a value below your max disk write speed. For example, to set it to 10MB/s, execute:

