This tutorial assumes you are running under isolation, which requires client-side retry handling for .
Step 1. Start CockroachDB
- Use CockroachDB Cloud
- Use a Local Cluster
Choose your installation method
You can create a CockroachDB Basic cluster using either the CockroachDB Cloud Console, a web-based graphical user interface (GUI) tool, orccloud, a command-line interface (CLI) tool.- Cloud Console (GUI)
- ccloud CLI
Create a free cluster
Organizations without billing information on file can only create one CockroachDB Basic cluster.
- If you haven’t already, sign up for a CockroachDB Cloud account.
- Log in to your CockroachDB Cloud account.
- On the Clusters page, click Create cluster.
- On the Select a plan page, select Basic.
- On the Cloud & Regions page, select a cloud provider (GCP or AWS) in the Cloud provider section.
- In the Regions section, select a region for the cluster. Refer to for the regions where CockroachDB Basic clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions.
- Click Next: Capacity.
- On the Capacity page, select Start for free. Click Next: Finalize.
- On the Finalize page, 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 sample application used in this tutorial uses system CA certificates for server certificate verification, so you can skip the Download CA Cert instructions.
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.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:
- Local Cluster
- CockroachDB Cloud
-
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:

