Skip to content

Table Operations ​

Operation: createTable ​

pgm.createTable( tablename, columns, options ) ​

IMPORTANT

Create a new table - postgres docs

Arguments ​

NameTypeDescription
tablenameNamename for the new table
columnsobjectcolumn names / options -- see column definitions section
optionsobjecttable options (optional)

Options ​

OptionTypeDescription
temporarybooleandefault false
ifNotExistsbooleandefault false
inheritsNametable(s) to inherit from
constraintsobjecttable constraints see expression of add constraint
likeName or objecttable(s) to inherit from or object with table and options keys
commentstringadds comment on table
unloggedbooleandefault false
partitionobjectpartition key: strategy (RANGE, LIST, or HASH) and columns (a column name or array of column names)

like options ​

OptionTypeDescription
includingstring or array[string]'COMMENTS', 'CONSTRAINTS', 'DEFAULTS', 'IDENTITY', 'INDEXES', 'STATISTICS', 'STORAGE', 'ALL'
excludingstring or array[string]'COMMENTS', 'CONSTRAINTS', 'DEFAULTS', 'IDENTITY', 'INDEXES', 'STATISTICS', 'STORAGE', 'ALL'

Declarative partitioning ​

createTable with partition creates the parent table. Use createPartition to create child partitions before inserting rows; PostgreSQL routes each row to the matching partition. Partition columns also accept objects with name, collate, and opclass. Collation and operator class strings are SQL fragments.

js
pgm.createTable(
  'events',
  { id: 'integer', created_at: 'date' },
  { partition: { strategy: 'RANGE', columns: 'created_at' } }
);
pgm.createPartition('events_2026', 'events', {
  strategy: 'RANGE',
  from: ['2026-01-01'],
  to: ['2027-01-01'],
});
pgm.createPartition('events_other', 'events', { strategy: 'DEFAULT' });

Reverse Operation: dropTable ​

pgm.dropTable( tablename, options ) ​

IMPORTANT

Drop existing table - postgres docs

Arguments ​

NameTypeDescription
tablenameNamename of the table to drop
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops table only if it exists
cascadebooleandrops also dependent objects

Operation: createPartition ​

pgm.createPartition( tablename, parent, bound, options ) ​

Creates a child table using PARTITION OF. Columns, defaults, constraints, and indexes are inherited from the parent. Automatic reversal detaches the child from its parent, then drops it using dropTable. Detaching preserves foreign keys referencing the parent and refuses to remove a partition containing referenced rows, even with cascade: true. Define the parent before its children so that automatic rollback removes the children first.

With ifExists: true, reversal skips detaching and uses DROP TABLE IF EXISTS directly, including when the child still exists. PostgreSQL has no IF EXISTS for the child in DETACH PARTITION. This allows rollback when the child or parent has already been removed; dropping an attached child can still fail if another table references its parent.

WARNING

DROP TABLE ... CASCADE on an attached partition can drop foreign keys referencing the entire parent. This applies to explicit dropTable calls and automatic reversal with both ifExists: true and cascade: true. Use the default detach-before-drop reversal to preserve these foreign keys.

NameTypeDescription
tablenameNamename of the new partition
parentNameexisting partitioned parent table
boundobjectpartition bounds, described below
optionsobjectoptional ifNotExists, temporary, unlogged, comment, and partition, as for createTable; ifExists and cascade apply to automatic reversal

Partition bounds ​

strategyOther propertiesMeaning
RANGEfrom: Value[], to: Value[]inclusive lower bound and exclusive upper bound, with one value per partition key column
LISTvalues: Value[]values assigned to this partition; null matches SQL NULL
HASHmodulus: number, remainder: numberinteger modulus from 1 to 2147483647 and integer remainder from zero to modulus minus one
DEFAULTnonerows not matched by another partition; available for range and list partitioning

strategy: 'DEFAULT' selects the default bound for a RANGE or LIST parent; it does not introduce another partitioning strategy.

Range bounds must be arrays, nonempty and equal in length. List values must be a nonempty array. Values are escaped as SQL values: strings remain string literals. Use pgm.func(...) for explicit SQL expressions, including MINVALUE and MAXVALUE in range bounds. PostgreSQL checks that bounds match the parent's partition strategy and key types and do not overlap existing partitions.

js
// For parents partitioned by LIST and HASH, respectively:
pgm.createPartition('orders_pending', 'orders', {
  strategy: 'LIST',
  values: ['pending', 'processing'],
});
pgm.createPartition('users_0', 'users', {
  strategy: 'HASH',
  modulus: 2,
  remainder: 0,
});
pgm.createPartition('users_1', 'users', {
  strategy: 'HASH',
  modulus: 2,
  remainder: 1,
});

Set options.partition to create a partition that is itself partitioned; create its children with further createPartition calls. To attach or detach an existing table, use pgm.sql('ALTER TABLE ... ATTACH PARTITION ...') or pgm.sql('ALTER TABLE ... DETACH PARTITION ...') with an explicit down migration.

Operation: renameTable ​

pgm.renameTable( tablename, new_tablename ) ​

IMPORTANT

Rename a table - postgres docs

Reverse Operation: same operation in opposite direction

Arguments ​

NameTypeDescription
tablenameNamename of the table to rename
new_tablenameNamenew name of the table

Renaming preserves the source schema, including during automatic reversal. See Renaming and schemas for destination-schema validation and supported PgLiteral identifiers.

To move a table between schemas, use SQL explicitly, for example pgm.sql('ALTER TABLE "old_schema"."my_table" SET SCHEMA "new_schema"'). Provide the corresponding SQL in your down migration to reverse that move.

Operation: alterTable ​

pgm.alterTable( tablename, options ) ​

IMPORTANT

Alter existing table - postgres docs

Arguments ​

NameTypeDescription
tablenameNamename of the table to alter
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
levelSecuritystringDISABLE, ENABLE, FORCE, or NO FORCE
unloggedbooleanSets UNLOGGED if true