Declare a table schema
Create a PostgreSQL schema in TypeScript with typed columns and constraints.
Drizzle ORM · Function
pgTable declares a typed PostgreSQL table from its database name and column definitions. Columns use drizzle-orm/pg-core builders, with optional callbacks for alternate column construction and table-level configuration.
pgTable creates a typed PostgreSQL table declaration from a database table name and a column-definition object. Columns are built with pg-core helpers, and the optional callback form can return table-level configuration such as indexes or constraints.
pgTable(name, columns, extraConfig?)| Name | Type | Description |
|---|---|---|
name | string | The PostgreSQL table name. |
columns | object | (tableBuilder) => object | An object mapping schema property names to PostgreSQL column builders, or a callback returning that object. |
extraConfig | (table) => config | Optional callback for table-level indexes, constraints, and related configuration. |
A typed PostgreSQL table object used in Drizzle schema declarations and queries.
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
name: varchar().notNull(),
age: integer().notNull(),
});Defines a PostgreSQL table and its typed columns in a Drizzle schema.
Create a PostgreSQL schema in TypeScript with typed columns and constraints.
Add table-level indexes or constraints through the optional configuration callback.
Apply a consistent snake_case or camelCase mapping to column keys.
pgTable declares a PostgreSQL table in a TypeScript schema. Its first argument is the database table name, and its second argument defines columns with PostgreSQL column builders.
Follow-up: How can table-level configuration be added? Use the optional third callback argument, which receives the table columns and returns configuration such as indexes or constraints.
The column keys become the schema property names, while each builder describes the corresponding PostgreSQL column type and modifiers such as primaryKey, notNull, or unique. This produces typed table metadata for Drizzle queries and schema tooling.
Follow-up: How would you define a database column name different from its property name? Pass the database column name to the column builder, such as text("first_name").
Import pgTable and column builders from drizzle-orm/pg-core, then export the table declaration so it can be used by the rest of the application. The documentation shows both a direct object form and a callback form for columns.
Follow-up: When might the callback form be useful? It lets column definitions be created through the supplied table builder and supports a table configuration callback for additional schema objects.
integervarchartextserialprimaryKeynotNulluniqueindex