Skip to main content
You can build applications with CockroachDB using various programming languages and frameworks. CockroachDB supports both native drivers and the PostgreSQL wire protocol, making it compatible with most PostgreSQL client drivers and ORM frameworks.

Getting Started

Building an application with CockroachDB involves three main steps:
1

Install a client driver or ORM

Choose a driver or ORM framework for your programming language. CockroachDB provides full or partial support for popular options across multiple languages.
2

Connect to your cluster

Use a connection string to establish a connection from your application to your CockroachDB cluster. Connection strings include your username, password, host, port, and database name.
3

Execute database operations

Perform CRUD operations (Create, Read, Update, Delete) using your chosen driver or ORM. Implement transaction retry logic for production applications.

Choose Your Language

Select a programming language to start building your application:

JavaScript/TypeScript

Build apps with Node.js using drivers like pg, or ORMs like Prisma, Sequelize, and TypeORM

Python

Use psycopg2/psycopg3 drivers or popular ORMs like SQLAlchemy and Django

Go

Connect with pgx or pq drivers, or use GORM for object-relational mapping

Java

Build enterprise apps with JDBC, Hibernate, or jOOQ frameworks

Ruby

Use the pg driver or Active Record ORM for Ruby applications

C# / .NET

Connect with the Npgsql driver for .NET applications

Example Applications

We provide complete, runnable example applications that demonstrate:
  • Establishing database connections
  • Creating tables and inserting data
  • Executing transactions with retry logic
  • Performing CRUD operations
  • Following best practices for production apps
Each example is available in a GitHub repository with instructions for running locally.

JavaScript/TypeScript Examples

A simple CRUD application using the node-postgres driver.
git clone https://github.com/cockroachlabs/example-app-node-postgres
cd example-app-node-postgres
npm install
node app.js
Features:
  • Connection pooling
  • Transaction retry logic with exponential backoff
  • INSERT, SELECT, UPDATE, and DELETE operations

Python Examples

Simple application using the psycopg2 driver.
pip install psycopg2-binary
python example.py
Key concepts:
  • Connection string configuration
  • Transaction management
  • Application-level retry logic with exponential backoff

Go Examples

High-performance PostgreSQL driver for Go.
import "github.com/jackc/pgx/v5"
Features:
  • Native Go implementation
  • Connection pooling
  • Prepared statements

Java Examples

Standard Java database connectivity.
./gradlew run
Best practices:
  • Use reWriteBatchedInserts=true for 2-3x performance improvement
  • Batch size of 128 for optimal performance
  • Connection pooling

Common Patterns

Connection String Format

Most drivers accept PostgreSQL-style connection strings:
postgresql://username:password@host:port/database?options

Transaction Retry Logic

CockroachDB may require clients to retry transactions due to contention. All production applications should implement retry logic:
async function retryTxn(txnFunc, maxRetries = 15) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await txnFunc();
    } catch (err) {
      if (err.code !== '40001') throw err;
      await sleep(Math.pow(2, i) * 100);
    }
  }
}

Next Steps

Client Drivers

View the complete list of supported drivers and ORMs

ORM Support

Learn about ORM frameworks and best practices

Best Practices

Follow recommended patterns for production applications

Performance Tuning

Optimize your application’s database queries

Build docs developers (and LLMs) love