Skip to main content

pgTable()

Defines a PostgreSQL table with columns and constraints.

Function Signatures

Parameters

string
required
Table name in the database
object | function
required
Column definitions as an object, or a function receiving column type builders
function
Function returning array of indexes, constraints, and checks

Basic Usage

Using Column Builder Function

Pass a function to receive all column type builders without individual imports:

Adding Indexes and Constraints

Use the third parameter to define indexes, foreign keys, and other constraints:

Foreign Keys

Composite Primary Keys

Check Constraints

pgSchema()

Defines a PostgreSQL schema (namespace) for organizing tables.
string
required
Schema name (cannot be ‘public’ - use pgTable directly for public schema)

Schema Methods

table()

Defines a table within the schema. Same API as pgTable().

view()

Defines a view within the schema.

materializedView()

Defines a materialized view within the schema.

enum()

Defines an enum within the schema.

sequence()

Defines a sequence within the schema.

pgTableCreator()

Creates a custom table creator with a name transformation function. Useful for adding prefixes or transforming table names.
(name: string) => string
required
Function that transforms table names

Use Cases

enableRLS()

Enables Row Level Security (RLS) on a table.
This generates SQL:

PgTable Class

The table object returned by pgTable() contains:

Properties

  • Column references for building queries
  • Table metadata (name, schema)
  • Type information for TypeScript inference

Type Inference

Accessing Table Information

Best Practices

Use Schemas for Organization

Define Constraints Explicitly