This tutorial assumes you are running under isolation, which requires client-side retry handling for .
Step 1. Start CockroachDB
Choose your installation method
You can install a CockroachDB Standard cluster using either the CockroachDB Cloud Console, a web-based graphical user interface (GUI) tool, orccloud, a command-line interface (CLI) tool.
Create a free trial cluster
- . If this is your first CockroachDB Cloud organization, it will be credited with $400 in to get you started.
- On the Get Started page, click Create cluster.
- On the Select a plan page, select Standard.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS).
- In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Standard clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, keep the at the default value of 2 vCPUs. Click Next: Finalize.
- On the Finalize page, name your cluster. If an active free trial is listed in the right pane, you will not need to add a payment method, though you will need to do this by the to maintain your organization’s clusters. Click Create cluster. Your cluster will be created in a few seconds and the Create SQL user dialog will display.
Create a SQL user
The Create SQL user dialog allows you to create a new SQL user and password.- Enter a username in the SQL user field or use the one provided by default.
- Click Generate & save password.
- Copy the generated password and save it in a secure location.
- Click Next. Currently, all new SQL users are created with admin privileges. For more information and to change the default settings, see .
Get the connection string
The Connect to cluster dialog shows information about how to connect to your cluster.- Select General connection string from the Select option dropdown.
- Open the General connection string section, then copy the connection string provided and save it in a secure location.
The connection string is pre-populated with your username, password, cluster name, and other details. Your password, in particular, will be provided only once. Save it in a secure place (Cockroach Labs recommends a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page for the cluster, found at
https://cockroachlabs.cloud/cluster/<CLUSTER ID>/users.ccloud CLI tool.
The
ccloud CLI tool is in Preview.Install ccloud
Choose your OS:
You can install ccloud using either Homebrew or by downloading the binary.
Use Homebrew
- Install Homebrew.
-
Install using the
ccloudtap:
Download the binary
In a terminal, enter the following command to download and extract theccloud binary and add it to your PATH:
ccloud binary and add it to your PATH:
ccloud binary and add it to your PATH:
Run ccloud quickstart to create a new cluster, create a SQL user, and retrieve the connection string.
The easiest way of getting started with CockroachDB Cloud is to use ccloud quickstart. The ccloud quickstart command guides you through logging in to CockroachDB Cloud, creating a new CockroachDB Basic cluster, and connecting to the new cluster. Run ccloud quickstart and follow the instructions:
ccloud quickstart command will open a browser window to log you in to CockroachDB Cloud. If you are new to CockroachDB Cloud, you can register using one of the single sign-on (SSO) options, or create a new account using an email address.
The ccloud quickstart command will prompt you for the cluster name, cloud provider, and cloud provider region, then ask if you want to connect to the cluster. Each prompt has default values that you can select, or change if you want a different option.
Select General connection string, then copy the connection string displayed and save it in a secure location. The connection string is the line starting postgresql://.
Step 2. Get the code
Clone the code’s GitHub repo:dbinit.sql file initializes the database schema that the application uses:
app.js file contains the code for INSERT, SELECT, UPDATE, and DELETE SQL operations:
retryTxn. This function attempts to commit statements in the context of an explicit transaction. If a is thrown, the wrapper will retry committing the transaction, with exponential backoff, until the maximum number of retries is reached (by default, 15).
Step 3. Initialize the database
-
Navigate to the
example-app-node-postgresdirectory: -
Set the
DATABASE_URLenvironment variable to the connection string for your cluster:Where is the connection string you copied earlier. -
To initialize the example database, use the command to execute the SQL statements in the
dbinit.sqlfile:The SQL statement in the initialization file should execute: - If you haven’t already, .
-
Run the command:
This starts an insecure, single-node cluster.
-
Take note of the following connection information in the SQL shell welcome text:
You’ll use the
sqlconnection string to connect to the cluster later in this tutorial.
The
--insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.Step 2. Get the code
Clone the code’s GitHub repo:dbinit.sql file initializes the database schema that the application uses:
app.js file contains the code for INSERT, SELECT, UPDATE, and DELETE SQL operations:
retryTxn. This function attempts to commit statements in the context of an explicit transaction. If a is thrown, the wrapper will retry committing the transaction, with exponential backoff, until the maximum number of retries is reached (by default, 15).
Step 3. Initialize the database
-
Navigate to the
example-app-node-postgresdirectory: -
Set the
DATABASE_URLenvironment variable to the connection string for your cluster: -
To initialize the example database, use the command to execute the SQL statements in the
dbinit.sqlfile:The SQL statement in the initialization file should execute:
Step 4. Run the code
-
Install the app requirements:
-
Run the app:
The app uses the connection string saved to the
DATABASE_URLenvironment variable to connect to your cluster and execute the code. The output should look like this:

