Connect to the Database (CockroachDB Cloud)

On this page Carat arrow pointing down
Warning:
CockroachDB v20.2 is no longer supported as of May 10, 2022. For more details, refer to the Release Support Policy.

This page has instructions for connecting to a CockroachDB Cloud 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 free CockroachDB Cloud 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 Serverless 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 dialog, 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 Serverless 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 dialog, 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 Serverless 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 dialog, 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 Serverless 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 dialog, your username, password, hostname, and cluster name will be pre-populated.

For complete examples, see:

Note:

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

What's next?

You might also be interested in the following pages:


Yes No
On this page

Yes No