PostgreSQL Column Types in Drizzle ORM
Complete guide to mapping PostgreSQL column types to Drizzle ORM pg-core column builders including serial, uuid, jsonb, timestamp with timezone, and more.
Detailed Explanation
PostgreSQL Type Mapping in Drizzle
Drizzle's drizzle-orm/pg-core module provides column builders that closely mirror PostgreSQL's native types. This gives you fine-grained control over exactly which database types are used.
Common Type Mappings
| PostgreSQL Type | Drizzle Builder | Example |
|---|---|---|
SERIAL |
serial() |
serial("id") |
BIGSERIAL |
bigserial() |
bigserial("id", { mode: "number" }) |
INTEGER |
integer() |
integer("count") |
SMALLINT |
smallint() |
smallint("age") |
BIGINT |
bigint() |
bigint("total", { mode: "number" }) |
VARCHAR(n) |
varchar() |
varchar("name", { length: 255 }) |
TEXT |
text() |
text("bio") |
BOOLEAN |
boolean() |
boolean("active") |
TIMESTAMP |
timestamp() |
timestamp("created_at") |
TIMESTAMPTZ |
timestamp() |
timestamp("created_at", { withTimezone: true }) |
DATE |
date() |
date("birth_date") |
UUID |
uuid() |
uuid("id") |
JSON |
json() |
json("data") |
JSONB |
jsonb() |
jsonb("metadata") |
NUMERIC(p,s) |
numeric() |
numeric("price", { precision: 10, scale: 2 }) |
REAL |
real() |
real("score") |
DOUBLE PRECISION |
doublePrecision() |
doublePrecision("lat") |
Example Conversion
CREATE TABLE products (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
name VARCHAR(200) NOT NULL,
price NUMERIC(10, 2) NOT NULL,
metadata JSONB,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
Produces:
import { jsonb, numeric, pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
export const products = pgTable("products", {
id: uuid("id").default(sql`gen_random_uuid()`).primaryKey(),
name: varchar("name", { length: 200 }).notNull(),
price: numeric("price", { precision: 10, scale: 2 }).notNull(),
metadata: jsonb("metadata"),
createdAt: timestamp("created_at", { withTimezone: true }).default(sql`CURRENT_TIMESTAMP`),
});
PostgreSQL-Specific Builders
Some builders are exclusive to the pg-core module: uuid(), jsonb(), doublePrecision(), smallint(), serial(), and bigserial(). These have no direct equivalents in the MySQL or SQLite modules and will be mapped to alternative types when switching dialects.
Use Case
You are building a PostgreSQL-backed application with Drizzle ORM and need to understand which pg-core column builders correspond to your existing PostgreSQL column types.