Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

CockroachDB (Prisma ORM v6) (/docs/orm/v6/overview/databases/cockroachdb)

For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending .md to its URL.

Guide to CockroachDB

Location: ORM > v6 > Core Concepts > Supported databases > CockroachDB

This guide discusses the concepts behind using Prisma ORM and CockroachDB, explains the commonalities and differences between CockroachDB and other database providers, and leads you through the process for configuring your application to integrate with CockroachDB.

[!NOTE] The CockroachDB connector is generally available in versions 3.14.0 and later. It was first added as a Preview feature in version 3.9.0 with support for Introspection, and Prisma Migrate support was added in 3.11.0.

CockroachDB is a distributed database that is designed for scalability and high availability. Features include:

  • Compatibility with PostgreSQL: CockroachDB is compatible with PostgreSQL, allowing interoperability with a large ecosystem of existing products
  • Built-in scaling: CockroachDB comes with automated replication, failover and repair capabilities to allow horizontal scaling of your application

CockroachDB is largely compatible with PostgreSQL, and can mostly be used with Prisma ORM in the same way. You can still:

There are some CockroachDB-specific differences to be aware of when working with Prisma ORM's cockroachdb connector:

This section provides more details on how to use CockroachDB-specific features.

CockroachDB has its own set of native data types which are supported in Prisma ORM. For example, CockroachDB uses the STRING data type instead of PostgreSQL's VARCHAR.

As a demonstration of this, say you create a User table in your CockroachDB database using the following SQL command:

SQL
CREATE TABLE public."Post" (
  "id" INT8 NOT NULL,
  "title" VARCHAR(200) NOT NULL,
  CONSTRAINT "Post_pkey" PRIMARY KEY ("id" ASC),
  FAMILY "primary" ("id", "title")
);

After introspecting your database with npx prisma db pull, you will have a new Post model in your Prisma Schema:

schema.prisma
model Post {
  id    BigInt @id
  title String @db.String(200)
}

Notice that the title field has been annotated with @db.String(200) — this differs from PostgreSQL where the annotation would be @db.VarChar(200).

For a full list of type mappings, see our connector documentation.

When generating unique identifiers for records in a distributed database like CockroachDB, it is best to avoid using sequential IDs – for more information on this, see CockroachDB's blog post on choosing index keys.

Instead, Prisma ORM provides the autoincrement() attribute function, which uses CockroachDB's unique_rowid() function for generating unique identifiers. For example, the following User model has an id primary key, generated using the autoincrement() function:

schema.prisma
model User {
  id   BigInt @id @default(autoincrement())
  name String
}

For compatibility with existing databases, you may sometimes still need to generate a fixed sequence of integer key values. In these cases, you can use Prisma ORM's inbuilt sequence() function for CockroachDB. For a list of available options for the sequence() function, see our reference documentation.

For more information on generating database keys, see CockroachDB's Primary key best practices guide.

To connect to a CockroachDB database server, you need to configure a datasource block in your Prisma schema:

schema.prisma
datasource db {
  provider = "cockroachdb"
}

The database connection URL is configured in prisma.config.ts:

prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    url: env("DATABASE_URL"),
  },
});

[!NOTE] When using Prisma CLI commands, environment variables are not automatically loaded. You'll need to use a package like dotenv to load environment variables from a .env file, or ensure your environment variables are set in your shell.

The fields passed to the datasource block are:

  • provider: Specifies the cockroachdb data source connector.
  • The url field is configured in prisma.config.ts and specifies the connection URL for the CockroachDB database server.

[!NOTE] While cockroachdb and postgresql connectors are similar, it is mandatory to use the cockroachdb connector instead of postgresql when connecting to a CockroachDB database from version 5.0.0.

CockroachDB uses the PostgreSQL format for its connection URL. See the PostgreSQL connector documentation for details of this format, and the optional arguments it takes.

Differences between CockroachDB and PostgreSQL

Section titled “Differences between CockroachDB and PostgreSQL”

The following table lists differences between CockroachDB and PostgreSQL:

Issue Area Notes
By default, the INT type is an alias for INT8 in CockroachDB, whereas in PostgreSQL it is an alias for INT4. This means that Prisma ORM will introspect an INT column in CockroachDB as BigInt, whereas in PostgreSQL Prisma ORM will introspect it as Int. Schema For more information on the INT type, see the CockroachDB documentation
When using @default(autoincrement()) on a field, CockroachDB will automatically generate 64-bit integers for the row IDs. These integers will be increasing but not consecutive. This is in contrast to PostgreSQL, where generated row IDs are consecutive and start from 1. Schema For more information on generated values, see the CockroachDB documentation
The @default(autoincrement()) attribute can only be used together with the BigInt field type. Schema For more information on generated values, see the CockroachDB documentation

The CockroachDB connector maps the scalar types from the Prisma ORM data model to native column types. These native types are mostly the same as for PostgreSQL — see the Native type mapping from Prisma ORM to CockroachDB for details. However, there are some limitations:

CockroachDB (Type | Aliases) Prisma ORM Supported Native database type attribute Notes
money Decimal Not yet @db.Money Supported in PostgreSQL but not currently in CockroachDB
xml String Not yet @db.Xml Supported in PostgreSQL but not currently in CockroachDB
jsonb arrays Json[] Not yet N/A Json[] supported in PostgreSQL but not currently in CockroachDB

The following table lists any other current known limitations of CockroachDB compared to PostgreSQL:

Issue Area Notes
Index types Hash, Gist, SpGist or Brin are not supported. Schema In PostgreSQL, Prisma ORM allows configuration of indexes to use the different index access method. CockroachDB only currently supports BTree and Gin.
Pushing to Enum types not supported Client Pushing to Enum types (e.g. data: { enum { push: "A" }, }) is currently not supported in CockroachDB
Searching on String fields without a full text index not supported Client Searching on String fields without a full text index (e.g. where: { text: { search: "cat & dog", }, },) is currently not supported in CockroachDB
Integer division not supported Client Integer division (e.g. data: { int: { divide: 10, }, }) is currently not supported in CockroachDB
Limited filtering on Json fields Client Currently CockroachDB only supports equals and not filtering on Json fields

Type mapping between CockroachDB and the Prisma schema

Section titled “Type mapping between CockroachDB and the Prisma schema”

The CockroachDB connector maps the scalar types from the Prisma ORM data model as follows to native column types:

Alternatively, see the Prisma schema reference for type mappings organized by Prisma ORM type.

Native type mapping from Prisma ORM to CockroachDB

Section titled “Native type mapping from Prisma ORM to CockroachDB”
Prisma ORM CockroachDB
String STRING
Boolean BOOL
Int INT4
BigInt INT8
Float FLOAT8
Decimal DECIMAL(65,30)
DateTime TIMESTAMP(3)
Json JSONB
Bytes BYTES

Mapping from CockroachDB to Prisma ORM types on Introspection

Section titled “Mapping from CockroachDB to Prisma ORM types on Introspection”

When introspecting a CockroachDB database, the database types are mapped to Prisma ORM according to the following table:

CockroachDB (Type | Aliases) Prisma ORM Supported Native database type attribute Notes
INT | BIGINT, INTEGER BigInt ✔️ @db.Int8
BOOL | BOOLEAN Bool ✔️ @db.Bool*
TIMESTAMP | TIMESTAMP WITHOUT TIME ZONE DateTime ✔️ @db.Timestamp(x)
TIMESTAMPTZ | TIMESTAMP WITH TIME ZONE DateTime ✔️ @db.Timestamptz(x)
TIME | TIME WITHOUT TIME ZONE DateTime ✔️ @db.Time(x)
TIMETZ | TIME WITH TIME ZONE DateTime ✔️ @db.Timetz(x)
DECIMAL(p,s) | NUMERIC(p,s), DEC(p,s) Decimal ✔️ @db.Decimal(x, y)
REAL | FLOAT4, FLOAT Float ✔️ @db.Float4
DOUBLE PRECISION | FLOAT8 Float ✔️ @db.Float8
INT2 | SMALLINT Int ✔️ @db.Int2
INT4 Int ✔️ @db.Int4
CHAR(n) | CHARACTER(n) String ✔️ @db.Char(x)
"char" String ✔️ @db.CatalogSingleChar Internal type for CockroachDB catalog tables, not meant for end users.
STRING | TEXT, VARCHAR String ✔️ @db.String
DATE DateTime ✔️ @db.Date
ENUM enum ✔️ N/A
INET String ✔️ @db.Inet
BIT(n) String ✔️ @Bit(x)
VARBIT(n) | BIT VARYING(n) String ✔️ @VarBit
OID Int ✔️ @db.Oid
UUID String ✔️ @db.Uuid
JSONB | JSON Json ✔️ @db.JsonB
Array types [] ✔️

Introspection adds native database types that are not yet supported as Unsupported fields:

schema.prisma
model Device {
  id       BigInt                  @id @default(autoincrement())
  interval Unsupported("INTERVAL")
}

The fastest way to start using CockroachDB with Prisma ORM is to refer to our Getting Started documentation:

These tutorials will take you through the process of connecting to CockroachDB, migrating your schema, and using Prisma Client.

Further reference information is available in the CockroachDB connector documentation.

  • Cloudflare D1: Guide to Cloudflare D1
  • Database drivers: Learn how Prisma connects to your database using the built-in drivers and how you can use Prisma along with other JavaScript database drivers using driver adapters (Preview)
  • Microsoft SQL Server: This page explains how Prisma can connect to a Microsoft SQL Server database using the Microsoft SQL Server database connector.
  • MySQL/MariaDB: This page explains how Prisma can connect to a MySQL or MariaDB database using the MySQL database connector.
  • Neon: Learn how to use Prisma ORM with Neon serverless PostgreSQL
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu