On this page
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.
CockroachDB Serverless
CockroachDB Dedicated
CockroachDB self-hosted
JavaScript/TypeScript
Python
Go
Java
Ruby
node-postgres
Sequelize
TypeORM
To connect to CockroachDB with node-postgres , create a new Client object with a connection string.
For example:
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:
postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&options=--cluster%3D<routing-id>
postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>
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 .
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 Sequelize , create a Sequelize object with the CockroachDB Sequelize adapter .
For example:
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:
postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&options=--cluster%3D<routing-id>
postgresql://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full&sslrootcert=<root-cert>
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:
postgresql://<username>:<password>@<host>:<port>/<routing-id>.<database>?ssl=true
postgresql://<username>:<password>@<host>:<port>/<database>?ssl=true&sslrootcert=<root-cert>
postgresql://<username>@<host>:<port>/<database>?ssl=true&sslrootcert=<root-cert>&sslcert=<client-cert>&sslkey=<client-key>
For more information about connecting with Sequelize, see the official Sequelize 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 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 CockroachDB Serverless requires you to specify the type, url, ssl, and options: "--cluster" properties:
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:
postgresql://<username>:<password>@<host>:<port>/<database>
CockroachDB CockroachDB Dedicated requires you to specify the type, url, and ssl properties:
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:
postgresql://<username>:<password>@<host>:<port>/<database>
CockroachDB CockroachDB self-hosted requires you to specify the type, url, and ssl properties:
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:
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:
import { AppDataSource } from " ./datasource " ;
AppDataSource . initialize ()
. then ( async () => {
// Execute operations
});
For more information about connecting with TypeORM, see the official TypeORM 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.
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.
Psycopg2
SQLAlchemy
Django
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:
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:
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
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:
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:
cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
cockroachdb://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
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:
## 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}'
}}}
...
## 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}')}}}
...
## 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}')}}}
...
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:
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:
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
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:
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:
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
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:
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:
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
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:
PGSimpleDataSource ds = new PGSimpleDataSource ();
ds . setUrl ( System . getenv ( "JDBC_DATABASE_URL" ));
Where JDBC_DATABASE_URL is an environment variable set to a valid CockroachDB connection string.
JDBC accepts the following format for CockroachDB connection strings:
jdbc:postgresql://{host}:{port}/{database}?options=--cluster%3D{routing-id}&password={password}&sslmode=verify-full&user={username}
jdbc:postgresql://{host}:{port}/{database}?user={username}&password={password}&sslmode=verify-full&sslrootcert={root-cert}
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:
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:
jdbc:postgresql://{host}:{port}/{database}?options=--cluster%3D{routing-id}&password={password}&sslmode=verify-full&user={username}
jdbc:postgresql://{host}:{port}/{database}?user={username}&password={password}&sslmode=verify-full&sslrootcert={root-cert}
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 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 .
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:
#!/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:
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
postgresql://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
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:
cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&options=--cluster%3D{routing-id}
cockroachdb://{username}:{password}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}
cockroachdb://{username}@{host}:{port}/{database}?sslmode=verify-full&sslrootcert={root-cert}&sslcert={client-cert}&sslkey={client-key}
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