SQL Dump (Export)

The cockroach dump command outputs the SQL statements required to recreate tables, views, and sequences. This command can be used to back up or export each database in a cluster. The output should also be suitable for importing into other relational databases, with minimal adjustments.

Tip:

CockroachDB enterprise license users can also back up their cluster's data using BACKUP.

Considerations

When cockroach dump is executed:

  • Table, sequence, and view schemas and table data are dumped as they appeared at the time that the command is started. Any changes after the command starts will not be included in the dump.
  • Table and view schemas are dumped in the order in which they can successfully be recreated. As of v2.0, this is true of sequences as well.
  • If the dump takes longer than the ttlseconds replication setting for the table (25 hours by default), the dump may fail.
  • Reads, writes, and schema changes can happen while the dump is in progress, but will not affect the output of the dump.

Note:

The user must have the SELECT privilege on the target table(s).

Known limitations

After using cockroach dump to dump the schema and data of an interleaved table, the output must be edited before it can be imported via IMPORT. See #35462 for the workaround and more details.

Synopsis

  1. # Dump the schemas and data of specific tables to stdout:
  2. $ cockroach dump <database> <table> <table...> <flags>
  3. # Dump just the data of specific tables to stdout:
  4. $ cockroach dump <database> <table> <table...> --dump-mode=data <other flags>
  5. # Dump just the schemas of specific tables to stdout:
  6. $ cockroach dump <database> <table> <table...> --dump-mode=schema <other flags>
  7. # Dump the schemas and data of all tables in a database to stdout:
  8. $ cockroach dump <database> <flags>
  9. # Dump just the schemas of all tables in a database to stdout:
  10. $ cockroach dump <database> --dump-mode=schema <other flags>
  11. # Dump just the data of all tables in a database to stdout:
  12. $ cockroach dump <database> --dump-mode=data <other flags>
  13. # Dump to a file:
  14. $ cockroach dump <database> <table> <flags> > dump-file.sql
  15. # View help:
  16. $ cockroach dump --help

Flags

The dump command supports the following general-use and logging flags.

General

FlagDescription
—as-ofDump table schema and/or data as they appear at the specified timestamp. See this example for a demonstration.Note that historical data is available only within the garbage collection window, which is determined by the ttlseconds replication setting for the table (25 hours by default). If this timestamp is earlier than that window, the dump will fail.Default: Current time
—dump-modeWhether to dump table and view schemas, table data, or both.To dump just table and view schemas, set this to schema. To dump just table data, set this to data. To dump both table and view schemas and table data, leave this flag out or set it to both.Table and view schemas are dumped in the order in which they can successfully be recreated. For example, if a database includes a table, a second table with a foreign key dependency on the first, and a view that depends on the second table, the dump will list the schema for the first table, then the schema for the second table, and then the schema for the view.Default: both
—echo-sqlReveal the SQL statements sent implicitly by the command-line utility.

Client connection

FlagDescription
—hostThe server host and port number to connect to. This can be the address of any node in the cluster. Env Variable: COCKROACH_HOSTDefault: localhost:26257
—port-pThe server port to connect to. Note: The port number can also be specified via —host. Env Variable: COCKROACH_PORTDefault: 26257
—user-uThe SQL user that will own the client session.Env Variable: COCKROACH_USERDefault: root
—insecureUse an insecure connection.Env Variable: COCKROACH_INSECUREDefault: false
—certs-dirThe path to the certificate directory containing the CA and client certificates and client key.Env Variable: COCKROACH_CERTS_DIRDefault: ${HOME}/.cockroach-certs/
—urlA connection URL to use instead of the other arguments.Env Variable: COCKROACH_URLDefault: no URL

See Client Connection Parameters for more details.

Note:

The user specified with —user must have the SELECT privilege on the target tables.

Logging

By default, the dump command logs errors to stderr.

If you need to troubleshoot this command's behavior, you can change its logging behavior.

Examples

Note:

These examples use our sample startrek database, which you can add to a cluster via the cockroach gen command. Also, the examples assume that the maxroach user has been granted the SELECT privilege on all target tables.

Dump a table's schema and data

  1. $ cockroach dump startrek episodes --insecure --user=maxroach > backup.sql
  1. $ cat backup.sql
  1. CREATE TABLE episodes (
  2. id INT NOT NULL,
  3. season INT NULL,
  4. num INT NULL,
  5. title STRING NULL,
  6. stardate DECIMAL NULL,
  7. CONSTRAINT "primary" PRIMARY KEY (id),
  8. FAMILY "primary" (id, season, num),
  9. FAMILY fam_1_title (title),
  10. FAMILY fam_2_stardate (stardate)
  11. );
  12. INSERT INTO episodes (id, season, num, title, stardate) VALUES
  13. (1, 1, 1, 'The Man Trap', 1531.1),
  14. (2, 1, 2, 'Charlie X', 1533.6),
  15. (3, 1, 3, 'Where No Man Has Gone Before', 1312.4),
  16. (4, 1, 4, 'The Naked Time', 1704.2),
  17. (5, 1, 5, 'The Enemy Within', 1672.1),
  18. (6, 1, 6, e'Mudd\'s Women', 1329.8),
  19. (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4),
  20. (8, 1, 8, 'Miri', 2713.5),
  21. (9, 1, 9, 'Dagger of the Mind', 2715.1),
  22. (10, 1, 10, 'The Corbomite Maneuver', 1512.2),
  23. ...

Dump just a table's schema

  1. $ cockroach dump startrek episodes --insecure --user=maxroach --dump-mode=schema > backup.sql
  1. $ cat backup.sql
  1. CREATE TABLE episodes (
  2. id INT NOT NULL,
  3. season INT NULL,
  4. num INT NULL,
  5. title STRING NULL,
  6. stardate DECIMAL NULL,
  7. CONSTRAINT "primary" PRIMARY KEY (id),
  8. FAMILY "primary" (id, season, num),
  9. FAMILY fam_1_title (title),
  10. FAMILY fam_2_stardate (stardate)
  11. );

Dump just a table's data

  1. $ cockroach dump startrek episodes --insecure --user=maxroach --dump-mode=data > backup.sql
  1. $ cat backup.sql
  1. INSERT INTO episodes (id, season, num, title, stardate) VALUES
  2. (1, 1, 1, 'The Man Trap', 1531.1),
  3. (2, 1, 2, 'Charlie X', 1533.6),
  4. (3, 1, 3, 'Where No Man Has Gone Before', 1312.4),
  5. (4, 1, 4, 'The Naked Time', 1704.2),
  6. (5, 1, 5, 'The Enemy Within', 1672.1),
  7. (6, 1, 6, e'Mudd\'s Women', 1329.8),
  8. (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4),
  9. (8, 1, 8, 'Miri', 2713.5),
  10. (9, 1, 9, 'Dagger of the Mind', 2715.1),
  11. (10, 1, 10, 'The Corbomite Maneuver', 1512.2),
  12. ...

Dump all tables in a database

  1. $ cockroach dump startrek --insecure --user=maxroach > backup.sql
  1. $ cat backup.sql
  1. CREATE TABLE episodes (
  2. id INT NOT NULL,
  3. season INT NULL,
  4. num INT NULL,
  5. title STRING NULL,
  6. stardate DECIMAL NULL,
  7. CONSTRAINT "primary" PRIMARY KEY (id),
  8. FAMILY "primary" (id, season, num),
  9. FAMILY fam_1_title (title),
  10. FAMILY fam_2_stardate (stardate)
  11. );
  12. CREATE TABLE quotes (
  13. quote STRING NULL,
  14. characters STRING NULL,
  15. stardate DECIMAL NULL,
  16. episode INT NULL,
  17. INDEX quotes_episode_idx (episode),
  18. FAMILY "primary" (quote, rowid),
  19. FAMILY fam_1_characters (characters),
  20. FAMILY fam_2_stardate (stardate),
  21. FAMILY fam_3_episode (episode)
  22. );
  23. INSERT INTO episodes (id, season, num, title, stardate) VALUES
  24. (1, 1, 1, 'The Man Trap', 1531.1),
  25. (2, 1, 2, 'Charlie X', 1533.6),
  26. (3, 1, 3, 'Where No Man Has Gone Before', 1312.4),
  27. (4, 1, 4, 'The Naked Time', 1704.2),
  28. (5, 1, 5, 'The Enemy Within', 1672.1),
  29. (6, 1, 6, e'Mudd\'s Women', 1329.8),
  30. (7, 1, 7, 'What Are Little Girls Made Of?', 2712.4),
  31. (8, 1, 8, 'Miri', 2713.5),
  32. (9, 1, 9, 'Dagger of the Mind', 2715.1),
  33. (10, 1, 10, 'The Corbomite Maneuver', 1512.2),
  34. ...
  35. INSERT INTO quotes (quote, characters, stardate, episode) VALUES
  36. ('"... freedom ... is a worship word..." "It is our worship word too."', 'Cloud William and Kirk', NULL, 52),
  37. ('"Beauty is transitory." "Beauty survives."', 'Spock and Kirk', NULL, 72),
  38. ('"Can you imagine how life could be improved if we could do away with jealousy, greed, hate ..." "It can also be improved by eliminating love, tenderness, sentiment -- the other side of the coin"', 'Dr. Roger Corby and Kirk', 2712.4, 7),
  39. ...

Dump fails (user does not have SELECT privilege)

In this example, the dump command fails for a user that does not have the SELECT privilege on the episodes table.

  1. $ cockroach dump startrek episodes --insecure --user=leslieroach > backup.sql
  1. Error: pq: user leslieroach has no privileges on table episodes
  2. Failed running "dump"

Restore a table from a backup file

In this example, a user that has the CREATE privilege on the startrek database uses the cockroach sql command to recreate a table, based on a file created by the dump command.

  1. $ cat backup.sql
  1. CREATE TABLE quotes (
  2. quote STRING NULL,
  3. characters STRING NULL,
  4. stardate DECIMAL NULL,
  5. episode INT NULL,
  6. INDEX quotes_episode_idx (episode),
  7. FAMILY "primary" (quote, rowid),
  8. FAMILY fam_1_characters (characters),
  9. FAMILY fam_2_stardate (stardate),
  10. FAMILY fam_3_episode (episode)
  11. );
  12. INSERT INTO quotes (quote, characters, stardate, episode) VALUES
  13. ('"... freedom ... is a worship word..." "It is our worship word too."', 'Cloud William and Kirk', NULL, 52),
  14. ('"Beauty is transitory." "Beauty survives."', 'Spock and Kirk', NULL, 72),
  15. ('"Can you imagine how life could be improved if we could do away with jealousy, greed, hate ..." "It can also be improved by eliminating love, tenderness, sentiment -- the other side of the coin"', 'Dr. Roger Corby and Kirk', 2712.4, 7),
  16. ...
  1. $ cockroach sql --insecure --database=startrek --user=maxroach < backup.sql
  1. CREATE TABLE
  2. INSERT 100
  3. INSERT 100

Dump table data as of a specific time

In this example, we assume there were several inserts into a table both before and after 2017-03-07 19:55:00.

First, let's use the built-in SQL client to view the table at the current time:

  1. $ cockroach sql --insecure --execute="SELECT * FROM db1.dump_test"
  1. +--------------------+------+
  2. | id | name |
  3. +--------------------+------+
  4. | 225594758537183233 | a |
  5. | 225594758537248769 | b |
  6. | 225594758537281537 | c |
  7. | 225594758537314305 | d |
  8. | 225594758537347073 | e |
  9. | 225594758537379841 | f |
  10. | 225594758537412609 | g |
  11. | 225594758537445377 | h |
  12. | 225594991654174721 | i |
  13. | 225594991654240257 | j |
  14. | 225594991654273025 | k |
  15. | 225594991654305793 | l |
  16. | 225594991654338561 | m |
  17. | 225594991654371329 | n |
  18. | 225594991654404097 | o |
  19. | 225594991654436865 | p |
  20. +--------------------+------+
  21. (16 rows)

Next, let's use a time-travel query to view the contents of the table as of 2017-03-07 19:55:00:

  1. $ cockroach sql --insecure --execute="SELECT * FROM db1.dump_test AS OF SYSTEM TIME '2017-03-07 19:55:00'"
  1. +--------------------+------+
  2. | id | name |
  3. +--------------------+------+
  4. | 225594758537183233 | a |
  5. | 225594758537248769 | b |
  6. | 225594758537281537 | c |
  7. | 225594758537314305 | d |
  8. | 225594758537347073 | e |
  9. | 225594758537379841 | f |
  10. | 225594758537412609 | g |
  11. | 225594758537445377 | h |
  12. +--------------------+------+
  13. (8 rows)

Finally, let's use cockroach dump with the —as-of flag set to dump the contents of the table as of 2017-03-07 19:55:00.

  1. $ cockroach dump db1 dump_test --insecure --dump-mode=data --as-of='2017-03-07 19:55:00'
  1. INSERT INTO dump_test (id, name) VALUES
  2. (225594758537183233, 'a'),
  3. (225594758537248769, 'b'),
  4. (225594758537281537, 'c'),
  5. (225594758537314305, 'd'),
  6. (225594758537347073, 'e'),
  7. (225594758537379841, 'f'),
  8. (225594758537412609, 'g'),
  9. (225594758537445377, 'h');

As you can see, the results of the dump are identical to the earlier time-travel query.

See also

Was this page helpful?
YesNo