apps/docs/content/docs/orm/core-concepts/supported-databases/postgresql.mdx
Prisma ORM supports PostgreSQL and PostgreSQL-compatible databases including self-hosted PostgreSQL, serverless providers (Neon, Supabase), and CockroachDB.
Configure the provider in your Prisma schema:
datasource db {
provider = "postgresql" // or "cockroachdb" for CockroachDB
}
Self-hosted PostgreSQL:
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DATABASE_URL"), // postgres://user:pass@host:5432/db
},
});
Serverless (Neon/Supabase):
Use separate URLs for CLI (direct) and runtime (pooled):
DATABASE_URL="postgres://user:pass@host-pooler:6543/db?pgbouncer=true"
DIRECT_URL="postgres://user:pass@host:5432/db"
import { defineConfig, env } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: env("DIRECT_URL"), // CLI uses direct connection
},
});
Use JavaScript database drivers via driver adapters:
Standard PostgreSQL with pg:
npm install @prisma/adapter-pg
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma";
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
Neon serverless:
npm install @prisma/adapter-neon
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "./generated/prisma";
const adapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL });
const prisma = new PrismaClient({ adapter });
Standard PostgreSQL server (9.6+).
postgresql://user:pass@host:5432/databaseprisma migrate dev for developmentConnection string arguments:
| Argument | Default | Description |
|---|---|---|
schema | public | PostgreSQL schema to use |
connect_timeout | 5 | Seconds to wait for connection (0 = no timeout) |
sslmode | prefer | TLS mode: prefer, disable, require |
sslcert | Path to server certificate | |
sslidentity | Path to PKCS12 certificate |
Serverless PostgreSQL with automatic scaling and branching.
postgres://user:[email protected]:5432/db-pooler to hostname for connection pooling (PgBouncer, 10k connections)Timeout configuration: Configure connection and pool timeouts via your driver adapter (e.g. connectionTimeoutMillis for pg).
Resources: Neon docs • Connection pooling
PostgreSQL hosting with built-in auth, storage, and real-time features.
Connection types:
db.[project-ref].supabase.co:54326543 with ?pgbouncer=true5432 on pooler hostKey features:
Resources: Supabase docs • Prisma integration
Distributed, PostgreSQL-compatible database designed for scalability and high availability.
provider = "cockroachdb" in schemapostgresql://user:pass@host:26257/databaseKey differences:
| Feature | PostgreSQL | CockroachDB |
|---|---|---|
| Native types | VARCHAR(n) | STRING(n) |
| ID generation | autoincrement() | Uses unique_rowid() |
| Sequential IDs | Recommended | Avoid (use autoincrement() instead) |
ID generation example:
model User {
id BigInt @id @default(autoincrement()) // Uses unique_rowid()
name String
}
For compatibility with existing databases, use sequence():
model User {
id Int @id @default(sequence())
name String
}
Resources: CockroachDB docs • Primary key best practices
| Prisma | PostgreSQL | CockroachDB |
|---|---|---|
String | text | STRING |
Boolean | boolean | BOOL |
Int | integer | INT4 |
BigInt | bigint | INT8 |
Float | double precision | FLOAT8 |
Decimal | decimal(65,30) | DECIMAL |
DateTime | timestamp(3) | TIMESTAMP |
Json | jsonb | JSONB |
Bytes | bytea | BYTES |
See full type mapping reference for complete details.
SSL connections:
DATABASE_URL="postgresql://user:pass@host:5432/db?sslmode=require&sslcert=./cert.pem"
sslmode=prefer (default) - Use TLS if availablesslmode=require - Require TLS or failsslmode=disable - No TLSSocket connections:
DATABASE_URL="postgresql://user:pass@localhost/db?host=/var/run/postgresql/"
Specifying schema with driver adapters:
const adapter = new PrismaPg(
{ connectionString: process.env.DATABASE_URL },
{ schema: "mySchema" }
);
Connection pool defaults (Prisma ORM v7):
Driver adapters use pg defaults which differ from v6:
0 (no timeout) vs v6's 5s10s vs v6's 300sSee connection pool guide for configuration.