PostgreSQL schema declarations
Create PostgreSQL schemas in TypeScript with Drizzle's pg-core builders.
Drizzle ORM · Type Alias
PgTable is the PostgreSQL-specific table type family used for Drizzle table objects created with pgTable. It is useful when typing helpers that accept PostgreSQL tables, while concrete schemas are declared with PostgreSQL builders.
PgTable is the PostgreSQL table type family represented by tables created with pgTable. Use it when constraining generic helpers or working with PostgreSQL schema objects, while defining concrete tables through the pgTable builder.
PgTableimport { integer, pgTable, text } from "drizzle-orm/pg-core";
const users = pgTable("users", {
id: integer().primaryKey(),
name: text().notNull(),
});
function acceptTable(table: typeof users) {
return table;
}
acceptTable(users);Defines a PostgreSQL table with Drizzle's dialect-specific table builder and passes its inferred table object to a typed function.
Create PostgreSQL schemas in TypeScript with Drizzle's pg-core builders.
Constrain a generic utility to accept PostgreSQL table objects.
Use declared tables as the basis for queries and migrations.
PgTable represents a PostgreSQL table type, while pgTable is the builder used to declare a concrete PostgreSQL table. The table's columns and inferred types come from the object passed to pgTable.
Follow-up: Why not use one shared table type for PostgreSQL and other databases? Drizzle uses dialect-specific table objects, so PostgreSQL tables belong to the PgTable family rather than a universal table abstraction.
Use PgTable when a generic function should accept PostgreSQL table objects. Define the actual schema with pgTable, choosing PostgreSQL column builders such as integer and text.
Follow-up: How would you make the helper portable? Introduce a dialect-specific abstraction or separate overloads, because the documentation says there is no common table object.
A Drizzle table definition must contain at least one column. PostgreSQL tables are declared with the PostgreSQL-specific pgTable builder rather than a database-neutral table constructor.
Follow-up: What should be checked before accepting a table from another dialect? Its table type and builders must match the intended dialect; a MySQL or SQLite table should not be treated as PgTable.
pgTablePgColumnpgSchemaPgEnum