Open a SQL Shell to a Temporary Cluster

The cockroach demo command starts a temporary, in-memory, single-node CockroachDB cluster, optionally with a pre-loaded dataset, and opens an interactive SQL shell to the cluster.

The in-memory cluster persists only as long as the SQL shell is open. As soon as the shell is exited, the cluster and all its data are permanently destroyed. This command is therefore recommended only as an easy way to experiment with the CockroachDB SQL dialect.

Synopsis

  1. # Start an interactive SQL shell:
  2. $ cockroach demo <flags>
  3. # Load a sample dataset and start an interactive SQL shell:
  4. $ cockroach demo <dataset> <flags>
  5. # Execute SQL from the command line:
  6. $ cockroach demo --execute="<sql statement>;<sql statement>" --execute="<sql-statement>" <flags>
  7. # Exit the interactive SQL shell:
  8. $ \q
  9. ctrl-d
  10. # View help:
  11. $ cockroach demo --help

Datasets

WorkloadDescription
bankA bank database, with one bank table containing account details.
introAn intro database, with one table, mytable, with a hidden message.
startrekA startrek database, with two tables, episodes and quotes.
tpccA tpcc database, with a rich schema of multiple tables.

Flags

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

General

FlagDescription
—echo-sqlReveal the SQL statements sent implicitly by the command-line utility.This can also be enabled within the interactive SQL shell via the \set echo shell command.
—execute-eExecute SQL statements directly from the command line, without opening a shell. This flag can be set multiple times, and each instance can contain one or more statements separated by semi-colons. If an error occurs in any statement, the command exits with a non-zero status code and further statements are not executed. The results of each statement are printed to the standard output (see —format for formatting options).
—formatHow to display table rows printed to the standard output. Possible values: tsv, csv, table, raw, records, sql, html.Default: table for sessions that output on a terminal; tsv otherwiseThis flag corresponds to the display_format client-side option for use in interactive sessions.
—safe-updatesDisallow potentially unsafe SQL statements, including DELETE without a WHERE clause, UPDATE without a WHERE clause, and ALTER TABLE … DROP COLUMN.Default: true for interactive sessions; false otherwisePotentially unsafe SQL statements can also be allowed/disallowed for an entire session via the sql_safe_updates session variable.
—setSet a client-side option before starting the SQL shell or executing SQL statements from the command line via —execute. This flag may be specified multiple times, once per option.After starting the SQL shell, the \set and unset commands can be use to enable and disable client-side options as well.

Logging

By default, the demo command logs errors to stderr.

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

SQL shell

All SQL shell commands, client-side options, help, and shortcuts supported by the cockroach sql command are also supported by the cockroach demo command.

Web UI

When the SQL shell connects to the in-memory cluster, it prints a welcome text with some tips and CockroachDB version and cluster details. Most of these details resemble the welcome text that gets printed when connecting cockroach sql to a permanent cluster. However, one unique detail to note is the Web UI link. For the duration of the cluster, you can open the Web UI for the cluster at this link.

  1. #
  2. # Welcome to the CockroachDB demo database!
  3. #
  4. # You are connected to a temporary, in-memory CockroachDB
  5. # instance. Your changes will not be saved!
  6. #
  7. # Web UI: http://127.0.0.1:60105
  8. #
  9. # Server version: CockroachDB CCL v2.1.0-alpha.20180702-281-g07a11b8e8c-dirty (x86_64-apple-darwin17.6.0, built 2018/07/08 14:00:29, go1.10.1) (same version as client)
  10. # Cluster ID: 61b41af6-fb2c-4d9a-8a91-0a31933b3d31
  11. #
  12. # Enter \? for a brief introduction.
  13. #
  14. root@127.0.0.1:60104/defaultdb>

Example

In these examples, we demonstrate how to start a shell with cockroach demo. For more SQL shell features, see the cockroach sql examples.

Start an interactive SQL shell

  1. $ cockroach demo
  1. > CREATE TABLE t1 (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name STRING);
  1. > INSERT INTO t1 (name) VALUES ('Tom Thumb');
  1. > SELECT * FROM t1;
  1. +--------------------------------------+-----------+
  2. | id | name |
  3. +--------------------------------------+-----------+
  4. | 5d2e6faa-a78f-4ef3-845f-6e174bbb41fa | Tom Thumb |
  5. +--------------------------------------+-----------+
  6. (1 row)
  7. Time: 9.539973ms
  1. > \q

Load a sample dataset and start an interactive SQL shell

  1. $ cockroach demo startrek
  1. > SHOW TABLES FROM startrek;
  1. > SELECT * FROM startrek.episodes WHERE stardate > 5500;
  1. id | season | num | title | stardate
  2. +----+--------+-----+-----------------------------------+----------+
  3. 60 | 3 | 5 | Is There in Truth No Beauty? | 5630.7
  4. 62 | 3 | 7 | Day of the Dove | 5630.3
  5. 64 | 3 | 9 | The Tholian Web | 5693.2
  6. 65 | 3 | 10 | Plato's Stepchildren | 5784.2
  7. 66 | 3 | 11 | Wink of an Eye | 5710.5
  8. 69 | 3 | 14 | Whom Gods Destroy | 5718.3
  9. 70 | 3 | 15 | Let That Be Your Last Battlefield | 5730.2
  10. 73 | 3 | 18 | The Lights of Zetar | 5725.3
  11. 74 | 3 | 19 | Requiem for Methuselah | 5843.7
  12. 75 | 3 | 20 | The Way to Eden | 5832.3
  13. 76 | 3 | 21 | The Cloud Minders | 5818.4
  14. 77 | 3 | 22 | The Savage Curtain | 5906.4
  15. 78 | 3 | 23 | All Our Yesterdays | 5943.7
  16. 79 | 3 | 24 | Turnabout Intruder | 5928.5
  17. (14 rows)
  1. > \q

Execute SQL from the command-line

  1. $ cockroach demo \
  2. --execute="CREATE TABLE t1 (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name STRING);" \
  3. --execute="INSERT INTO t1 (name) VALUES ('Tom Thumb');" \
  4. --execute="SELECT * FROM t1;"
  1. CREATE TABLE
  2. INSERT 1
  3. +--------------------------------------+-----------+
  4. | id | name |
  5. +--------------------------------------+-----------+
  6. | 53476f43-d737-4506-ad83-4469c977f77c | Tom Thumb |
  7. +--------------------------------------+-----------+
  8. (1 row)

See also

Was this page helpful?
YesNo