Connect to the Database (CockroachDB Cloud)

This page has instructions for connecting to a CockroachDB Serverless (beta) cluster from your application using various programming languages. Each example shows a connection string for a secure cluster to a bank database. Depending on your cluster's configuration, you may need to edit this connection string.

The connection strings listed on this page set the required authentication options to connect to CockroachDB Serverless (beta) clusters. CockroachDB Cloud clusters use a signed certificate generated for your cluster that you download from the CockroachDB Cloud Console.

For a reference that lists all of the supported cluster connection parameters, see Connection Parameters.

Before you begin

Do the following:

Connect

icon/buttons/copy
$ cockroach sql \
--url='postgres://{username}:{password}@{globalhost}:26257/{cluster_name}.{database}?sslmode=verify-full&sslrootcert={path to the CA certificate}'

Where:

  • {username} and {password} specify the SQL username and password that you created earlier.
  • {globalhost} is the name of the CockroachDB Cloud free tier host (e.g., free-tier.gcp-us-central1.cockroachlabs.cloud).
  • {path to the CA certificate} is the path to the cc-ca.crt file that you downloaded from the CockroachDB Cloud Console.
  • {cluster_name} is the name of your cluster.
Note:

If you are using the connection string that you copied from the Connection info modal, your username, password, hostname, and cluster name will be pre-populated.

For more information about how to use the built-in SQL client, see the cockroach sql reference docs.

icon/buttons/copy
import (
    "database/sql"
    "fmt"
    "log"
    _ "github.com/lib/pq"
)

db, err := sql.Open("postgres",
        "postgresql://{username}:{password}@{globalhost}:26257/bank?sslmode=verify-full&sslrootcert={path to the CA certificate}&options=--cluster={cluster_name}")
if err != nil {
    log.Fatal("error connecting to the database: ", err)
}
defer db.Close()

Where:

  • {username} and {password} specify the SQL username and password that you created earlier.
  • {globalhost} is the name of the CockroachDB Cloud free tier host (e.g., free-tier.gcp-us-central1.cockroachlabs.cloud).
  • {path to the CA certificate} is the path to the cc-ca.crt file that you downloaded from the CockroachDB Cloud Console.
  • {cluster_name} is the name of your cluster.
Note:

If you are using the connection string that you copied from the Connection info modal, your username, password, hostname, and cluster name will be pre-populated.

For complete examples, see:

icon/buttons/copy
import java.sql.*;
import javax.sql.DataSource;

PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerName("{globalhost}");
ds.setPortNumber(26257);
ds.setDatabaseName("{cluster_name}.bank");
ds.setUser("{username}");
ds.setPassword("{password}");
ds.setSsl(true);
ds.setSslMode("verify-full");
ds.setSslrootCert("{path to the CA certificate}");
ds.setReWriteBatchedInserts(true); // add `rewriteBatchedInserts=true` to pg connection string
ds.setApplicationName("BasicExample");

Where:

  • {username} and {password} specify the SQL username and password that you created earlier.
  • {globalhost} is the name of the CockroachDB Cloud free tier host (e.g., free-tier.gcp-us-central1.cockroachlabs.cloud).
  • {path to the CA certificate} is the path to the cc-ca.crt file that you downloaded from the CockroachDB Cloud Console.
  • {cluster_name} is the name of your cluster.
Note:

If you are using the connection string that you copied from the Connection info modal, your username, password, hostname, and cluster name will be pre-populated.

For complete examples, see:

icon/buttons/copy
import psycopg2

conn = psycopg2.connect(
    database='bank',
    user='{username}',
    password='{password}'
    sslmode='verify-full',
    sslrootcert='{path to the CA certificate}',
    port=26257,
    host='{globalhost}',
    options="--cluster={cluster_name}"
)

Where:

  • {username} and {password} specify the SQL username and password that you created earlier.
  • {globalhost} is the name of the CockroachDB Cloud free tier host (e.g., free-tier.gcp-us-central1.cockroachlabs.cloud).
  • {path to the CA certificate} is the path to the cc-ca.crt file that you downloaded from the CockroachDB Cloud Console.
  • {cluster_name} is the name of your cluster.
Note:

If you are using the connection string that you copied from the Connection info modal, your username, password, hostname, and cluster name will be pre-populated.

For complete examples, see:

Note:

Cockroach Cloud does not yet support certificate-based user authentication.

What's next?

You might also be interested in the following pages:

YesYes NoNo