Getting Started
Building an application with CockroachDB involves three main steps: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.
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.
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
JavaScript/TypeScript Examples
- Node.js (pg)
- Prisma
A simple CRUD application using the node-postgres driver.Features:
- Connection pooling
- Transaction retry logic with exponential backoff
- INSERT, SELECT, UPDATE, and DELETE operations
Python Examples
- psycopg2
- SQLAlchemy
Simple application using the psycopg2 driver.Key concepts:
- Connection string configuration
- Transaction management
- Application-level retry logic with exponential backoff
Go Examples
- pgx Driver
- GORM
High-performance PostgreSQL driver for Go.Features:
- Native Go implementation
- Connection pooling
- Prepared statements
Java Examples
- JDBC
- Hibernate
Standard Java database connectivity.Best practices:
- Use
reWriteBatchedInserts=truefor 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:Transaction Retry Logic
CockroachDB may require clients to retry transactions due to contention. All production applications should implement retry logic: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