Connect to a CockroachDB Cluster

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.

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 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&options=--cluster%3D<routing-id>

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>

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 versions 6.10 and later accept the following format for CockroachDB connection strings:

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&options=--cluster%3D<routing-id>

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>

For Sequelize versions 6.9 and earlier, use the following format:

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<routing-id>.<database>?ssl=true

icon/buttons/copy

postgresql://<username>:<password>@<host>:<port>/<database>?ssl=true&sslrootcert=<root-cert>

icon/buttons/copy

postgresql://<username>@<host>:<port>/<database>?ssl=true&sslrootcert=<root-cert>&sslcert=<client-cert>&sslkey=<client-key>

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 (beta) requires you to specify the type, url, ssl, and options: "--cluster" properties:

icon/buttons/copy

import { DataSource } from "typeorm"

export const AppDataSource = new DataSource({
    type: "cockroachdb",
    url: process.env.DATABASE_URL,
    ssl: true,
    extra: {
        options: "--cluster=<routing-id>"
    },
    ...
});

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:

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&options=--cluster%3D<routing-id>

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>

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.
<routing-id> Your cluster's routing ID (e.g., funky-skunk-123). The routing ID identifies your tenant cluster on a multi-tenant host.
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:

To connect to a CockroachDB Serverless (beta) 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 (beta) 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&options=--cluster%3D{routing-id}

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}

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&options=--cluster%3D{routing-id}

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:

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.

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',
            'options': '--cluster={routing-id}'
        },
    },
}

...

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:

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.
{routing-id} Your cluster's routing ID (e.g., funky-skunk-123). The routing ID identifies your tenant cluster on a multi-tenant host.
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.

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&options=--cluster%3D{routing-id}

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}

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&options=--cluster%3D{routing-id}

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}

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&options=--cluster%3D{routing-id}

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}

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.
{routing-id} Your cluster's routing ID (e.g., funky-skunk-123). The routing ID identifies your tenant cluster on a multi-tenant host.
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.

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}?options=--cluster%3D{routing-id}&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}

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}?options=--cluster%3D{routing-id}&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:

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.
{routing-id} Your cluster's routing ID (e.g., funky-skunk-123). The routing ID identifies your tenant cluster on a multi-tenant host.
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:

To connect to a CockroachDB Serverless (beta) 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 (beta) 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&options=--cluster%3D{routing-id}

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}

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

To connect to CockroachDB with ActiveRecord 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.

ActiveRecord accepts the following format for CockroachDB connection strings:

icon/buttons/copy

cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}

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:

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

For more information about connecting with ActiveRecord, see the official ActiveRecord 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.
{routing-id} Your cluster's routing ID (e.g., funky-skunk-123). The routing ID identifies your tenant cluster on a multi-tenant host.
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.

See also


Yes No