Skip to main content
CockroachDB is a distributed SQL database built on a transactional and strongly-consistent key-value store. It scales horizontally, survives disk, machine, rack, and even datacenter failures with minimal latency disruption and no manual intervention.
Key features:
  • Distributed SQL: Scale out while maintaining ACID guarantees
  • Native vector support: Built-in VECTOR type (v24.2+) and C-SPANN indexes (v25.2+)
  • PostgreSQL compatible: Drop-in replacement for PostgreSQL applications
  • Global replication: Multi-region deployments with low latency
  • Automatic sharding: Data automatically distributed across nodes
  • SERIALIZABLE isolation: Strongest isolation level by default

Installation and Setup

Install the LangChain integration:
pip install langchain-cockroachdb

Get your CockroachDB connection string

You’ll need a CockroachDB cluster. Choose one option: Option 1: CockroachDB Cloud (Recommended)
  1. Sign up at cockroachlabs.cloud
  2. Create a free cluster
  3. Get your connection string: cockroachdb://user:pass@host:26257/db?sslmode=verify-full
Option 2: Docker (Development)
docker run -d --name cockroachdb -p 26257:26257 \
  cockroachdb/cockroach:latest start-single-node --insecure
Connection string: cockroachdb://root@localhost:26257/defaultdb?sslmode=disable Option 3: Local Binary Download from cockroachlabs.com/docs/releases

Integrations

Vector Store

CockroachDB can be used as a vector store with native VECTOR type and C-SPANN distributed indexes. Key features:
  • Native vector support (v24.2+)
  • C-SPANN indexes optimized for distributed systems (v25.2+)
  • Advanced metadata filtering
  • Multi-tenancy with prefix columns
  • Horizontal scalability
See CockroachDB vector store documentation for detailed usage. Quick example:
from langchain_cockroachdb import AsyncCockroachDBVectorStore, CockroachDBEngine
from langchain_openai import OpenAIEmbeddings

# Initialize
engine = CockroachDBEngine.from_connection_string(
    "cockroachdb://user:pass@host:26257/db"
)

await engine.ainit_vectorstore_table(
    table_name="documents",
    vector_dimension=1536,
)

vectorstore = AsyncCockroachDBVectorStore(
    engine=engine,
    embeddings=OpenAIEmbeddings(),
    collection_name="documents",
)

# Use it
ids = await vectorstore.aadd_texts(["Hello world"])
results = await vectorstore.asimilarity_search("Hi", k=1)

Chat Message History

Store conversation history in CockroachDB for persistent, distributed chat applications. Key features:
  • Distributed storage with automatic replication
  • Strong consistency (SERIALIZABLE)
  • Session-based organization
  • High availability
See CockroachDB chat history documentation for detailed usage. Quick example:
from langchain_cockroachdb import CockroachDBChatMessageHistory
import uuid

chat_history = CockroachDBChatMessageHistory(
    session_id=str(uuid.uuid4()),
    connection_string=CONNECTION_STRING,
    table_name="chat_history",
)

from langchain.messages import HumanMessage, AIMessage

await chat_history.aadd_message(HumanMessage(content="Hello!"))
await chat_history.aadd_message(AIMessage(content="Hi there!"))

messages = await chat_history.aget_messages()

Why CockroachDB for AI applications?

Distributed by design

  • Horizontal scalability: Add nodes to handle more load
  • Multi-region deployments: Serve users globally with low latency
  • Automatic rebalancing: Data distributes automatically across nodes

Production-ready reliability

  • High availability: Survives node, rack, and datacenter failures
  • Zero-downtime upgrades: Rolling updates without downtime
  • Backups and restores: Point-in-time recovery

Vector search at scale

  • C-SPANN indexes: Distributed approximate nearest neighbor search
  • Native vector type: First-class support for embeddings
  • Real-time indexing: No rebuild needed for new vectors
  • Multi-tenancy: Prefix columns for efficient tenant isolation

PostgreSQL compatibility

  • Easy migration: Drop-in replacement for PostgreSQL
  • Familiar SQL: Standard PostgreSQL syntax
  • Existing tools: Works with PostgreSQL drivers and tools

Resources

Support


Connect these docs to Claude, VSCode, and more via MCP for real-time answers.