Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Function

pgTable

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.

How it works

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.

Syntax

pgTable(name, columns, extraConfig?)

Parameters

NameTypeDescription
namestringThe PostgreSQL table name.
columnsobject | (tableBuilder) => objectAn object mapping schema property names to PostgreSQL column builders, or a callback returning that object.
extraConfig(table) => configOptional callback for table-level indexes, constraints, and related configuration.

Returns

A typed PostgreSQL table object used in Drizzle schema declarations and queries.

Key points

  • Use TypeScript declarations as the source for PostgreSQL table structure and supported column properties.
  • A table declaration starts by passing the database table name and a column map to pgTable.
  • Column builders can chain modifiers to express keys and identity-generated values.
  • A PostgreSQL varchar column can be declared and made required with notNull().
  • The callback column form receives a table builder object for constructing the table's columns.
  • Use the documented naming builders when table column keys should follow snake_case or camelCase conventions.

Common mistakes

  • Column property names are not automatically converted to a naming convention unless you use the documented snakeCase or camelCase builder.

Example: Define a PostgreSQL table

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.

When to use it

Declare a table schema

Create a PostgreSQL schema in TypeScript with typed columns and constraints.

Configure indexes and constraints

Add table-level indexes or constraints through the optional configuration callback.

Use naming conventions

Apply a consistent snake_case or camelCase mapping to column keys.

Interview questions

QWhat does pgTable do?

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.

QHow are columns represented in a pgTable declaration?

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").

QHow do you declare a table in a Drizzle PostgreSQL schema?

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.

  • integer
  • varchar
  • text
  • serial
  • primaryKey
  • notNull
  • unique
  • index

Source: Schema - Drizzle ORM. Checked 2026-10-16.