Skip to content

View Operations ​

Operation: createView ​

pgm.createView( viewName, options, definition ) ​

IMPORTANT

Create a new view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the new view
optionsobjectCheck below for available options
definitionstringSQL of SELECT statement

Options ​

OptionTypeDescription
temporarybooleanadds TEMPORARY clause
replacebooleanadds OR REPLACE clause
recursivebooleanadds RECURSIVE clause
columnsstring or arrayuse if you want to name columns differently then inferred from definition
optionsobjectkey value pairs of View Options
checkOptionstringCASCADED or LOCAL

Option names are quoted and folded to lowercase, without camel-case conversion. Values may be strings, finite numbers, or booleans. Strings are escaped as SQL values; pass check_option: 'local', without adding SQL quotes around local. The check_option enum is normalized to lowercase so information_schema.views reports LOCAL or CASCADED consistently with the checkOption convenience option. Other string values retain their supplied case.

NaN, Infinity, and -Infinity throw a TypeError naming the option and value during SQL generation, before SQL for that operation is queued. Finite numbers, including negatives, zero, and decimals, are rendered unchanged; PostgreSQL validates which values each option supports.

js
pgm.createView(
  'active_accounts',
  { options: { check_option: 'local', security_barrier: true } },
  'SELECT * FROM accounts WHERE active'
);

Reverse Operation: dropView ​

pgm.dropView( viewName, options ) ​

IMPORTANT

Drop a view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to delete
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops view only if it exists
cascadebooleandrops also dependent objects

Operation: alterView ​

pgm.alterView( viewName, options ) ​

IMPORTANT

Alter a view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to alter
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
checkOptionstringCASCADED, LOCAL or null to reset
optionsobjectkey value (null to reset) pairs of View Options

Option names and values use the same escaping and finite-number validation as createView. A null value resets the named option; it is not included in SET. For example:

js
pgm.alterView('active_accounts', {
  options: { security_barrier: false, check_option: null },
});

Operation: alterViewColumn ​

pgm.alterViewColumn( viewName, columnName, options ) ​

IMPORTANT

Alter a view column - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to alter
columnNamestringname of the column to alter
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
defaultstringdefault value of column

Operation: renameView ​

pgm.renameView( viewName, newViewName ) ​

IMPORTANT

Rename a view - postgres docs

Arguments ​

NameTypeDescription
viewNameNameold name of the view
newViewNameNamenew name of the view

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

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