This page shows you how to use the CockroachDB Cloud Console to create a CockroachDB Serverless cluster and then insert and read some sample data from a Java or Node.js sample application.
Create a CockroachDB Serverless cluster
Organizations without billing information on file can only create one CockroachDB Serverless 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 Serverless.
- 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 CockroachDB Cloud Regions for the regions where CockroachDB Serverless clusters can be deployed. To create a multi-region cluster, click Add region and select additional regions. A cluster can have at most six 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 Managing SQL users on a cluster.
Connect to the cluster
Select a language to connect a sample application to your cluster. To connect to your cluster directly from the command line, refer to Connect to a Serverless Cluster.
Once you create a SQL user, the Connect to cluster dialog will show information about how to connect to your cluster.
- Select Java from the Select option/language dropdown.
Copy the
JDBC_DATABASE_URL
environment variable command provided and save it in a secure location.This Quickstart uses default certificates, so you can skip the Download CA Cert instructions.
Note: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, a Cluster Administrator can reset it. Refer to: Managing SQL users on a cluster
Configure the connection environment variable
In a terminal set the JDBC_DATABASE_URL
environment variable to the JDBC connection string:
export JDBC_DATABASE_URL="<jdbc-connection-string>"
The code sample uses the connection string stored in the environment variable JDBC_DATABASE_URL
to connect to your cluster.
In a terminal set the JDBC_DATABASE_URL
environment variable to the JDBC connection string:
$env:JDBC_DATABASE_URL = "<jdbc-connection-string>"
The code sample uses the connection string stored in the environment variable JDBC_DATABASE_URL
to connect to your cluster.
Run the Java sample code
Clone the
quickstart-code-samples
repo:git clone https://github.com/cockroachdb/quickstart-code-samples
Navigate to the
java
directory of the repo:cd quickstart-code-samples/java
The code sample in this directory does the following:
- Connects to CockroachDB Cloud with the JDBC driver using the JDBC connection string set in the
JDBC_DATABASE_URL
environment variable. - Creates a table.
- Inserts some data into the table.
- Reads the inserted data.
- Prints the data to the terminal.
- Connects to CockroachDB Cloud with the JDBC driver using the JDBC connection string set in the
Run the application using
gradlew
:./gradlew run
The output should look like this:
> Task :app:run Hello world! BUILD SUCCESSFUL in 3s 2 actionable tasks: 2 executed
Once you create a SQL user, the Connect to cluster dialog will show 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.
This Quickstart uses default certificates, so you can skip the Download CA Cert instructions.
Note: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, a Cluster Administrator can reset it. Refer to: Managing SQL users on a cluster
Configure the connection environment variable
In a terminal set the DATABASE_URL
environment variable to the connection string:
export DATABASE_URL="<connection-string>"
The code sample uses the connection string stored in the environment variable DATABASE_URL
to connect to your cluster.
In a terminal set the DATABASE_URL
environment variable to the connection string:
$env:DATABASE_URL = "<connection-string>"
The code sample uses the connection string stored in the environment variable DATABASE_URL
to connect to your cluster.
Run the Node.js sample code
Clone the
quickstart-code-samples
repo:git clone https://github.com/cockroachdb/quickstart-code-samples
Navigate to the
node
directory of the repo:cd quickstart-code-samples/node
The code sample in this directory does the following:
- Connects to CockroachDB Cloud with the node-postgres driver using the connection string set in the
DATABASE_URL
environment variable. - Creates a table.
- Inserts some data into the table.
- Reads the inserted data.
- Prints the data to the terminal.
- Connects to CockroachDB Cloud with the node-postgres driver using the connection string set in the
Install the app requirements:
$ npm install
Run the app:
$ node app.js
The output will look like this:
Hello world!
Learn more
Now that you have a free CockroachDB Serverless cluster running, try out the following:
- Build a simple CRUD application in Go, Java, Node.js, or Python.
- Learn CockroachDB SQL.
- Create and manage SQL users.
- Explore our example apps for examples on how to build applications using your preferred driver or ORM and run it on CockroachDB.
- Migrate your existing data.
This page highlights just one way you can get started with CockroachDB. For information on other options that are available when creating a CockroachDB cluster, see the following:
- To create a Self-Hosted cluster, see Start a Local Cluster.
- To create a CockroachDB Dedicated cluster, see Quickstart with CockroachDB Dedicated.
- To create a CockroachDB Serverless cluster with other configurations (e.g., a different cloud provider, region, or monthly budget), see Create a CockroachDB Serverless Cluster.
- To connect to a CockroachDB Serverless cluster with other options (e.g., a different SQL user) and connection methods (with an application or CockroachDB compatible tool), see Connect to a CockroachDB Serverless Cluster.