Quickstart with CockroachDB

On this page Carat arrow pointing down

This page shows you how to get started with CockroachDB quickly. You'll use the CockroachDB Cloud Console to create a CockroachDB Serverless cluster and then insert and read some sample data from a Java sample application.

Create a CockroachDB Serverless cluster

Note:

Organizations without billing information on file can only create one CockroachDB Serverless cluster.

  1. If you haven't already, sign up for a CockroachDB Cloud account.
  2. Log in to your CockroachDB Cloud account.
  3. On the Clusters page, click Create Cluster.
  4. On the Create your cluster page, select Serverless.
  5. 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.

  1. Enter a username in the SQL user field or use the one provided by default.
  2. Click Generate & save password.
  3. Copy the generated password and save it in a secure location.
  4. 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

Once you create a SQL user, the Connect to cluster dialog will show information about how to connect to your cluster.

  1. Select Java from the Select option/language dropdown.
  2. 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:

icon/buttons/copy
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:

icon/buttons/copy
$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

  1. Clone the quickstart-code-samples repo:

    icon/buttons/copy
    git clone https://github.com/cockroachdb/quickstart-code-samples
    
  2. Navigate to the java directory of the repo:

    icon/buttons/copy
    cd quickstart-code-samples/java
    

    The code sample in this directory does the following:

    1. Connects to CockroachDB Cloud with the JDBC driver using the JDBC connection string set in the JDBC_DATABASE_URL environment variable.
    2. Creates a table.
    3. Inserts some data into the table.
    4. Reads the inserted data.
    5. Prints the data to the terminal.
  3. Run the application using gradlew:

    icon/buttons/copy
    ./gradlew run
    

    The output should look like this:

    > Task :app:run
    Hello world!
    
    BUILD SUCCESSFUL in 3s
    2 actionable tasks: 2 executed
    

Learn more

Now that you have a free CockroachDB Serverless cluster running, try out the following:

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:


Yes No
On this page

Yes No