title | summary | toc |
---|---|---|
Open a SQL Shell to a Temporary Cluster |
Use cockroach demo to open a SQL shell to a temporary, in-memory, single-node CockroachDB cluster. |
true |
The cockroach demo
command starts a temporary, in-memory 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.
# Start an interactive SQL shell:
$ cockroach demo <flags>
# Load a sample dataset and start an interactive SQL shell:
$ cockroach demo <dataset> <flags>
# Execute SQL from the command line:
$ cockroach demo --execute="<sql statement>;<sql statement>" --execute="<sql-statement>" <flags>
# Exit the interactive SQL shell:
$ \q
ctrl-d
# View help:
$ cockroach demo --help
Workload | Description |
---|---|
bank |
A bank database, with one bank table containing account details. |
intro |
An intro database, with one table, mytable , with a hidden message. |
movr |
A movr database, with several tables of data for the MovR example application. |
startrek |
A startrek database, with two tables, episodes and quotes . |
tpcc |
A tpcc database, with a rich schema of multiple tables. |
The demo
command supports the following general-use and logging flags.
Flag | Description |
---|---|
--demo-locality |
Specify locality information for each demo node. The input is a colon-separated list of key-value pairs, where the ith pair is the locality setting for the ith demo cockroach node. For example, the following option assigns node 1's region to us-east1 and availability zone to 1 , node 2's region to us-east2 and availability zone to 2 , and node 3's region to us-east3 and availability zone to 3 : --demo-locality=region=us-east1,az=1:region=us-east1,az=2:region=us-east1,az=3 . You can also include |
--echo-sql |
Reveal 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 -e |
Execute 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). |
--format |
How 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. |
--nodes |
Specify the number of in-memory nodes to create for the demo. Default: 1 |
--safe-updates |
Disallow 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. |
--set |
Set 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. |
By default, the demo
command logs errors to stderr
.
If you need to troubleshoot this command's behavior, you can change its logging behavior.
All SQL shell commands, client-side options, help, and shortcuts supported by the cockroach sql
command are also supported by the cockroach demo
command.
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.
#
# Welcome to the CockroachDB demo database!
#
# You are connected to a temporary, in-memory CockroachDB
# instance. Your changes will not be saved!
#
# Web UI: http://127.0.0.1:60105
#
# 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)
# Cluster ID: 61b41af6-fb2c-4d9a-8a91-0a31933b3d31
#
# Enter \? for a brief introduction.
#
root@127.0.0.1:60104/defaultdb>
In these examples, we demonstrate how to start a shell with cockroach demo
. For more SQL shell features, see the cockroach sql
examples.
{% include copy-clipboard.html %}
$ cockroach demo
{% include copy-clipboard.html %}
> CREATE TABLE drivers (
id UUID DEFAULT gen_random_uuid(),
city STRING NOT NULL,
name STRING,
dl STRING UNIQUE,
address STRING,
CONSTRAINT primary_key PRIMARY KEY (city ASC, id ASC)
);
{% include copy-clipboard.html %}
> INSERT INTO drivers (city, name) VALUES ('new york', 'Catherine Nelson');
{% include copy-clipboard.html %}
> SELECT * FROM drivers;
id | city | name | dl | address
+--------------------------------------+----------+------------------+------+---------+
df3dc272-b572-4ca4-88c8-e9974dbd381a | new york | Catherine Nelson | NULL | NULL
(1 row)
{% include copy-clipboard.html %}
> \q
{% include copy-clipboard.html %}
$ cockroach demo movr --nodes=3 --demo-locality=region=us-east1:region=us-central1:region=us-west1
{% include copy-clipboard.html %}
> SHOW TABLES;
table_name
+----------------------------+
promo_codes
rides
user_promo_codes
users
vehicle_location_histories
vehicles
(6 rows)
{% include copy-clipboard.html %}
> SELECT * FROM users WHERE city = 'new york';
id | city | name | address | credit_card
+--------------------------------------+----------+------------------+-----------------------------+-------------+
00000000-0000-4000-8000-000000000000 | new york | Robert Murphy | 99176 Anderson Mills | 8885705228
051eb851-eb85-4ec0-8000-000000000001 | new york | James Hamilton | 73488 Sydney Ports Suite 57 | 8340905892
0a3d70a3-d70a-4d80-8000-000000000002 | new york | Judy White | 18580 Rosario Ville Apt. 61 | 2597958636
0f5c28f5-c28f-4c00-8000-000000000003 | new york | Devin Jordan | 81127 Angela Ferry Apt. 8 | 5614075234
147ae147-ae14-4b00-8000-000000000004 | new york | Catherine Nelson | 1149 Lee Alley | 0792553487
(5 rows)
{% include copy-clipboard.html %}
> \q
{% include copy-clipboard.html %}
$ cockroach demo \
--execute="CREATE TABLE drivers (
id UUID DEFAULT gen_random_uuid(),
city STRING NOT NULL,
name STRING,
dl STRING UNIQUE,
address STRING,
CONSTRAINT primary_key PRIMARY KEY (city ASC, id ASC)
);" \
--execute="INSERT INTO drivers (city, name) VALUES ('new york', 'Catherine Nelson');" \
--execute="SELECT * FROM drivers;"
CREATE TABLE
INSERT 1
id | city | name | dl | address
+--------------------------------------+----------+------------------+------+---------+
df3dc272-b572-4ca4-88c8-e9974dbd381a | new york | Catherine Nelson | NULL | NULL
(1 row)