Connect to a CockroachDB Cluster

On this page Carat arrow pointing down
Warning:
GA releases for CockroachDB v23.1 are no longer supported. Cockroach Labs will stop providing LTS Assistance Support for v23.1 LTS releases on November 13, 2025. Prior to that date, upgrade to a more recent version to continue receiving support. For more details, refer to the Release Support Policy.

This page documents the required connection configuration for fully-supported third-party tools.

For a list of all supported cluster connection parameters, see the cockroach Connection Parameters.

For a list of community-supported third-party tools, see Third-Party Tools Supported by the Community. CockroachDB supports both native drivers and the PostgreSQL wire protocol. Most client drivers and ORM frameworks connect to CockroachDB like they connect to PostgreSQL.

Step 1. Select your deployment

To connect to a CockroachDB Cloud cluster, you need a general connection string or connection parameters, which include the username, host, database, and port. To find these, open the Connect dialog for your cluster in the CockroachDB Cloud Console and select either General connection string or Parameters only as the option.

To connect to a CockroachDB Self-Hosted cluster, you need the general connection string or connection parameters for your cluster.

The connection strings and parameters for your cluster are output when you start the cluster.

Step 2. Select your OS

Step 3. Select your language

Step 4. Select your driver or ORM

Step 5. Connect to the cluster

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

export DATABASE_URL="{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

$env:DATABASE_URL = "{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

export DATABASE_URL="{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

$env:DATABASE_URL = "{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

export DATABASE_URL="{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

$env:DATABASE_URL = "{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

export DATABASE_URL="{connection string}"

Set a DATABASE_URL environment variable to your connection string.

icon/buttons/copy

$env:DATABASE_URL = "{connection string}"

The Connect to cluster dialog shows information about how to connect to your cluster.

  1. Select Java from the Select option dropdown.
  2. Copy the JDBC_DATABASE_URL environment variable command provided and save it in a secure location.

Copy the JDBC connection string from the sql (JDBC) field in the output from when you started the cluster.

Set a JDBC_DATABASE_URL environment variable to your JDBC connection string.

icon/buttons/copy

export JDBC_DATABASE_URL="{connection string}"

icon/buttons/copy

$env:JDBC_DATABASE_URL = "{connection string}"

To connect to CockroachDB with node-postgres, create a new Client object with a connection string.

For example:

icon/buttons/copy

const { Client } = require('pg')

const client = new Client(process.env.DATABASE_URL)

client.connect()

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

node-postgres accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>

icon/buttons/copy

postgresql://<username>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>&sslcert=<client-cert>&sslkey=<client-key>
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with node-postgres, see the official node-postgres documentation.

To connect to CockroachDB with Sequelize, create a Sequelize object with the CockroachDB Sequelize adapter.

For example:

icon/buttons/copy

const Sequelize = require("sequelize-cockroachdb");

const connectionString = process.env.DATABASE_URL
const sequelize = new Sequelize(connectionString)

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Sequelize accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>

icon/buttons/copy

postgresql://<username>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>&sslcert=<client-cert>&sslkey=<client-key>
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to CockroachDB with Sequelize, you must install the CockroachDB Sequelize adapter.

For more information about connecting with Sequelize, see the official Sequelize documentation.

To connect to CockroachDB with TypeORM, update your project's DataSource with the required connection properties.

For example, suppose that you are defining the DataSource for your application in a file named datasource.ts.

CockroachDB Serverless requires you to specify the type, url, and ssl properties:

icon/buttons/copy

import { DataSource } from "typeorm"

export const AppDataSource = new DataSource({
    type: "cockroachdb",
    url: process.env.DATABASE_URL,
    ssl: true,
    ...
});

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

TypeORM accepts the following format for CockroachDB connection strings:

icon/buttons/copy
postgresql://<username>:<password>@<host>:<port>/<database>

CockroachDB Dedicated requires you to specify the type, url, and ssl properties:

icon/buttons/copy

import { DataSource } from "typeorm"

export const AppDataSource = new DataSource({
    type: "cockroachdb",
    url: process.env.DATABASE_URL,
    ssl: {
      ca: process.env.CA_CERT
    },
    ...
});

Where:

TypeORM accepts the following format for CockroachDB connection strings:

icon/buttons/copy
postgresql://<username>:<password>@<host>:<port>/<database>

CockroachDB Self-Hosted requires you to specify the type, url, and ssl properties:

icon/buttons/copy

import { DataSource } from "typeorm"

export const AppDataSource = new DataSource({
    type: "cockroachdb",
    url: process.env.DATABASE_URL,
    ssl: {
      ca: process.env.CA_CERT,
      key: process.env.CLIENT_KEY,
      cert: process.env.CLIENT_CERT
    },
  ...
});

Where:

Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

TypeORM accepts the following format for CockroachDB connection strings:

icon/buttons/copy
postgresql://<username>@<host>:<port>/<database>

You can then import the AppDataSource into any file in your project and call AppDataSource.initialize() to connect to CockroachDB:

icon/buttons/copy
import { AppDataSource } from "./datasource";

AppDataSource.initialize()
  .then(async () => {
    // Execute operations
  });

For more information about connecting with TypeORM, see the official TypeORM documentation.

To connect to CockroachDB with Prisma, set the url field of the datasource block in your Prisma schema to your database connection URL:

icon/buttons/copy

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "cockroachdb"
  url      = env("DATABASE_URL")
}

model Widget {
  id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
}

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Prisma accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>

icon/buttons/copy

postgresql://<username>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>&sslcert=<client-cert>&sslkey=<client-key>
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with Prisma, see the official Prisma documentation.

Connection parameters

Parameter Description
<username> The SQL user connecting to the cluster.
<password> The password for the SQL user connecting to the cluster.
<host> The host on which the CockroachDB node is running.
<port> The port at which the CockroachDB node is listening.
<database> The name of the (existing) database.
Parameter Description
<username> The SQL user connecting to the cluster.
<password> The password for the SQL user connecting to the cluster.
<host> The host on which the CockroachDB node is running.
<port> The port at which the CockroachDB node is listening.
<database> The name of the (existing) database.
<root-cert> The path to the root certificate that you downloaded from the CockroachDB Cloud Console.
Parameter Description
<username> The SQL user connecting to the cluster.
<host> The host on which the CockroachDB node is running.
<port> The port at which the CockroachDB node is listening.
<database> The name of the (existing) database.
<root-cert> The path to the root certificate.
You can generate this certificate with cockroach cert create-ca, or you can use a custom CA cert.
<client-cert> The path to the client certificate for the user connecting to the cluster.
You can generate this certificate with cockroach cert create-client.
<client-key> The path to the client key for the user connecting to the cluster.
You can generate this key with cockroach cert create-client.
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to a CockroachDB Serverless cluster from a Python application, you must have a valid CA certificate located at ~/.postgresql/root.crt.
For instructions on downloading a CA certificate from the CockroachDB Cloud Console, see Connect to a CockroachDB Serverless Cluster.

To connect to CockroachDB with Psycopg2, pass a connection string to the psycopg2.connect function.

For example:

icon/buttons/copy

import psycopg2
import os

conn = psycopg2.connect(os.environ['DATABASE_URL'])

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Psycopg2 accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with Psycopg, see the official Psycopg documentation.

To connect to CockroachDB with Psycopg3, pass a connection string to the psycopg.connect function.

For example:

icon/buttons/copy

import psycopg
import os

with psycopg.connect(os.environ['DATABASE_URL']) as conn:
  # application logic here

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Psycopg accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with Psycopg, see the official Psycopg documentation.

To connect to CockroachDB with SQLAlchemy, create an Engine object by passing the connection string to the create_engine function.

For example:

icon/buttons/copy

from sqlalchemy import create_engine
import os

engine = create_engine(os.environ['DATABASE_URL'])
engine.connect()

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

SQLAlchemy accepts the following format for CockroachDB connection strings:

icon/buttons/copy

cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

cockroachdb://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to CockroachDB with SQLAlchemy, you must install the CockroachDB SQLAlchemy adapter.

For more information about connecting with SQLAlchemy, see the official SQLAlchemy documentation.

Note:

In order for SQLAlchemy to use the CockroachDB adapter, the connection string must begin with cockroachdb://. You can use the following code to modify the general connection string, which begins with postgresql://, to the format that works with SQLAlchemy and the CockroachDB adapter:

icon/buttons/copy
engine = create_engine(os.environ['DATABASE_URL'].replace("postgresql://", "cockroachdb://"))
engine.connect()

To connect to CockroachDB from a Django application, update the DATABASES property in the project's settings.py file.

Django accepts the following format for CockroachDB connection information:

icon/buttons/copy

## settings.py

...

DATABASES = {
    'default': {
        'ENGINE': 'django_cockroachdb',
        'NAME': '{database}',
        'USER': '{username}',
        'PASSWORD': '{password}',
        'HOST': '{host}',
        'PORT': '{port}',
        'OPTIONS': {
            'sslmode': 'verify-full'
        },
    },
}

...

icon/buttons/copy

## settings.py

...

DATABASES = {
    'default': {
        'ENGINE': 'django_cockroachdb',
        'NAME': '{database}',
        'USER': '{username}',
        'PASSWORD': '{password}',
        'HOST': '{host}',
        'PORT': '{port}',
        'OPTIONS': {
            'sslmode': 'verify-full',
            'sslrootcert': os.path.expandvars('{root-cert}'),
        },
    },
}

...

icon/buttons/copy

## settings.py

...

DATABASES = {
    'default': {
        'ENGINE': 'django_cockroachdb',
        'NAME': '{database}',
        'USER': '{username}',
        'HOST': '{host}',
        'PORT': '{port}',
        'OPTIONS': {
            'sslmode': 'verify-full',
            'sslrootcert': os.path.expandvars('{root-cert}'),
            'sslcert': os.path.expandvars('{client-cert}'),
            'sslkey': os.path.expandvars('{client-key}'),
        },
    },
}

...
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to CockroachDB with Django, you must install the CockroachDB Django adapter.

For more information about connecting with Django, see the official Django documentation.

Connection parameters

Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
Note:

Earlier connection strings or connection parameters to CockroachDB Serverless clusters used a routing ID to identify the cluster on the host server. For example, in the connection string the options query parameter had cluster={routing-id}. This is no longer necessary, as the cluster's routing ID is part of the {host} parameter.

Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate that you downloaded from the CockroachDB Cloud Console.
Parameter Description
{username} The SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate.
You can generate this certificate with cockroach cert create-ca, or you can use a custom CA cert.
{client-cert} The path to the client certificate for the user connecting to the cluster.
You can generate this certificate with cockroach cert create-client.
{client-key} The path to the client key for the user connecting to the cluster.
You can generate this key with cockroach cert create-client.
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

To connect to CockroachDB with pgx, use the pgx.Connect function.

For example:

icon/buttons/copy

package main

import (
    "context"
    "log"

    "github.com/jackc/pgx/v4"
)

func main() {
    conn, err := pgx.Connect(context.Background(), "<connection-string>")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close(context.Background())
}

pgx accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with pgx, see the official pgx documentation.

To connect to CockroachDB with pq, use the sql.Open function.

For example:

icon/buttons/copy

package main

import (
    "database/sql"
    "log"

    _ "github.com/lib/pq"
)

func main() {
    db, err := sql.Open("postgres", "<connection-string>")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
}

pq accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with pq, see the official pq documentation.

To connect to CockroachDB with GORM, use the gorm.Open function, with the GORM postgres driver.

For example:

icon/buttons/copy

package main

import (
    "log"

    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

func main() {
    db, err := gorm.Open(postgres.Open("<connection-string>"), &gorm.Config{})
    if err != nil {
        log.Fatal(err)
    }
}

GORM accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with GORM, see the official GORM documentation.

Connection parameters

Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate that you downloaded from the CockroachDB Cloud Console.
Parameter Description
{username} The SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate.
You can generate this certificate with cockroach cert create-ca, or you can use a custom CA cert.
{client-cert} The path to the client certificate for the user connecting to the cluster.
You can generate this certificate with cockroach cert create-client.
{client-key} The path to the client key for the user connecting to the cluster.
You can generate this key with cockroach cert create-client.
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

To connect to CockroachDB with the JDBC driver, create a DataSource object (PGSimpleDataSource or PGPoolingDataSource), and set the connection string with the setUrl class method.

For example:

icon/buttons/copy

PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setUrl(System.getenv("JDBC_DATABASE_URL"));

Where JDBC_DATABASE_URL is an environment variable set to a valid JDBC-compatible CockroachDB connection string.

JDBC accepts the following format for CockroachDB connection strings:

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?password={password}&sslmode=verify-full&user={username}

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?user={username}&password={password}&sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?user={username}&sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with JDBC, see the official JDBC documentation.

To connect to CockroachDB with Hibernate ORM, set the object's hibernate.connection.url property to a valid CockroachDB connection string.

For example, if you are bootstrapping your application with a ServiceRegistry object, first read from the Hibernate configuration file, and then set the hibernate.connection.url with the applySetting class method:

icon/buttons/copy

StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
    .configure( "hibernate.cfg.xml" ).applySetting("hibernate.connection.url", System.getenv("DATABASE_URL"))
    .build();

Metadata metadata = new MetadataSources( standardRegistry )
    .getMetadataBuilder()
    .build();

SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
    .build();

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Hibernate accepts the following format for CockroachDB connection strings:

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?password={password}&sslmode=verify-full&user={username}

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?user={username}&password={password}&sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

jdbc:postgresql://{host}:{port}/{database}?user={username}&sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to CockroachDB with Hibernate, you must specify the CockroachDB Hibernate dialect in your hibernate.cfg.xml configuration file.

For more information about connecting with Hibernate, see the official Hibernate documentation.

Connection parameters

Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The URL-encoded path to the root certificate that you downloaded from the CockroachDB Cloud Console.
Parameter Description
{username} The SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The URL-encoded path to the root certificate.
You can generate this certificate with cockroach cert create-ca, or you can use a custom CA cert.
{client-cert} The URL-encoded path to the client certificate for the user connecting to the cluster.
You can generate this certificate with cockroach cert create-client.
{client-key} The URL-encoded path to the PKCS#8-formatted client key for the user connecting to the cluster.
You can generate this key with cockroach cert create-client --also-generate-pkcs8-key.
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to a CockroachDB Serverless cluster from a Ruby application, you must have a valid CA certificate located at ~/.postgresql/root.crt.
For instructions on downloading a CA certificate from the CockroachDB Cloud Console, see Connect to a CockroachDB Serverless Cluster.

To connect to CockroachDB with the Ruby pg driver, use the PG.connect function.

For example:

icon/buttons/copy

#!/usr/bin/env ruby

require 'pg'

conn = PG.connect(ENV['DATABASE_URL'])

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

pg accepts the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

postgresql://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

For more information about connecting with pg, see the official pg documentation.

To connect to CockroachDB with Active Record from a Rails app, update the database configuration in config/database.yml:

default: &default
  adapter: cockroachdb
  url: <%= ENV['DATABASE_URL'] %>

...

Where DATABASE_URL is an environment variable set to a valid CockroachDB connection string.

Active Record accepts the following format for CockroachDB connection strings:

icon/buttons/copy

cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full

icon/buttons/copy

cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}

icon/buttons/copy

cockroachdb://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

Note:

To connect to CockroachDB with Active Record, you must install the Active Record CockroachDB adapter.

For more information about connecting with Active Record, see the official Active Record documentation.

Connection parameters

Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
Parameter Description
{username} The SQL user connecting to the cluster.
{password} The password for the SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate that you downloaded from the CockroachDB Cloud Console.
Parameter Description
{username} The SQL user connecting to the cluster.
{host} The host on which the CockroachDB node is running.
{port} The port at which the CockroachDB node is listening.
{database} The name of the (existing) database.
{root-cert} The path to the root certificate.
You can generate this certificate with cockroach cert create-ca, or you can use a custom CA cert.
{client-cert} The path to the client certificate for the user connecting to the cluster.
You can generate this certificate with cockroach cert create-client.
{client-key} The path to the client key for the user connecting to the cluster.
You can generate this key with cockroach cert create-client.
Note:

The connection information shown on this page uses client certificate and key authentication to connect to a secure, CockroachDB Self-Hosted cluster.

To connect to a CockroachDB Self-Hosted cluster with client certificate and key authentication, you must first generate server and client certificates.

For instructions on starting a secure cluster, see Start a Local Cluster (Secure).

See also


Yes No
On this page

Yes No