This page shows you how to deploy a CockroachDB cluster on CockroachCloud (free for a 30-day trial for your first cluster), connect to it using a sample workload, and run your first query.
To run CockroachDB on your local machine instead, see Start a Local Cluster.
Before you begin
If you haven't already, sign up for a CockroachCloud account.
Step 1. Create a free cluster
For this tutorial, we will create a 3-node GCP cluster in the us-west2
region.
- Log in to your CockroachCloud account.
- On the Overview page, click Create Cluster.
- On the Create new cluster page, for Cloud provider, select Google Cloud.
- For Regions & nodes, use the default selection of
California (us-west)
region and 3 nodes. - For Hardware per node, select
Option 1
(2vCPU, 60 GB disk). - Name the cluster. The cluster name must be 6-20 characters in length, and can include lowercase letters, numbers, and dashes (but no leading or trailing dashes).
- Click Next.
On the Summary page, enter your credit card details.
Note:You won't be charged until after your free trial expires in 30 days.
Click Create cluster.
Your cluster will be created in approximately 20-30 minutes. Watch this Getting Started with CockroachCloud video while you wait.
Once your cluster is created, you will be redirected to the Cluster Overview page.
Step 2. Create a SQL user
- In the left navigation bar, click SQL Users.
- Click Add User. The Add User dialog displays.
- Enter a Username and Password.
- Click Save.
Step 3. Authorize your network
- In the left navigation bar, click Networking.
- Click Add Network. The Add Network dialog displays.
- From the Network dropdown, select Current Network to auto-populate your local machine's IP address.
- To allow the network to access the cluster's DB Console and to use the CockroachDB client to access the databases, select the DB Console to monitor the cluster and CockroachDB Client to access the databases checkboxes.
- Click Apply.
Step 4. Get the connection string
- In the top-right corner of the Console, click the Connect button. The Connect dialog displays.
- From the User dropdown, select the SQL user you created in Step 2. Create a SQL user.
- Verify that the
us-west2 GCP
region anddefault_db
database are selected. - Click Continue. The Connect tab is displayed.
- Click Connection string to get the connection string for your cluster.
- Create a
certs
directory on your local workstation. - Click the name of the
ca.crt
file to download the CA certificate to your local machine. - Move the downloaded
ca.crt
file to thecerts
directory.
Step 5. Run your first query
For this tutorial, we will use the movr
workload to run the first query. On your local machine:
Download the CockroachDB binary:
$ curl https://binaries.cockroachdb.com/cockroach-v20.2.8.darwin-10.9-amd64.tgz \ | tar -xJ
$ wget -qO- https://binaries.cockroachdb.com/cockroach-v20.2.8.linux-amd64.tgz \ | tar xvz
Copy the binary into the
PATH
so it's easy to run the SQL client from any location:$ cp -i cockroach-v20.2.8.darwin-10.9-amd64/cockroach /usr/local/bin/
$ sudo cp -i cockroach-v20.2.8.linux-amd64/cockroach /usr/local/bin/
Initialize the
movr
workload using thecockroach workload
command and the connection string.In the connection string, the SQL user's username is prepopulated. Replace
<password>
with the SQL user's password that you entered in Step 2. Replace the<certs_dir>
placeholder with the path to thecerts
directory that you created in Step 4.$ cockroach workload init movr \ 'postgres://<username>:<password>@<global host>:26257/movr?sslmode=verify-full&sslrootcert=<certs_dir>/<ca.crt>'
Use the built-in SQL client to view the database:
$ cockroach sql \ --url='postgres://<username>:<password>@<global host>:26257/movr?sslmode=verify-full&sslrootcert=<certs_dir>/<ca.crt>'
> SHOW TABLES FROM movr;
table_name ------------------------------ promo_codes rides user_promo_codes users vehicle_location_histories vehicles (6 rows)
Run your first query:
> SELECT * FROM movr.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 19999999-9999-4a00-8000-000000000005 | new york | Nicole Mcmahon | 11540 Patton Extensions | 0303726947 (6 rows)
What's next?
Learn more:
- Use the built-in SQL client to connect to your cluster and learn CockroachDB SQL.
- Build a "Hello World" app with the Django framework, or install a client driver for your favorite language.
- Use a local cluster to explore CockroachDB capabilities like fault tolerance and automated repair.
Before you move into production:
- Authorize the network from which your app will access the cluster.
- Download the
ca.crt
file to every machine from which you want to connect to the cluster. - Review the production checklist.