Skip to content

Policies Operations ​

Operation: createPolicy ​

pgm.createPolicy( tableName, policyName, options ) ​

IMPORTANT

Create a new policy - postgres docs

Arguments ​

NameTypeDescription
tableNameNamename of the table to alter
policyNamestringname of the new policy
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
asstringPERMISSIVE (default) or RESTRICTIVE
commandstringALL, SELECT, INSERT, UPDATE, or DELETE
rolestring or arraythe role(s) to which the policy is to be applied
usingstringSQL conditional expression for visibility check
checkstringSQL conditional expression for insert/update check
commentstring or nullComment on the policy

Permissive policies combine with OR; restrictive policies combine with AND. At least one permissive policy must grant access for restrictive policies to allow any rows. Omitting as preserves PostgreSQL's default permissive behavior.

js
pgm.createPolicy('accounts', 'active_accounts', {
  as: 'RESTRICTIVE',
  command: 'SELECT',
  using: 'active = true',
  comment: 'Only active accounts are visible',
});

Reversing createPolicy drops the policy and its comment.

The policy mode can only be set by createPolicy. To change it, drop and recreate the policy; alterPolicy cannot change whether a policy is permissive or restrictive.

Reverse Operation: dropPolicy ​

pgm.dropPolicy( tableName, policyName, options ) ​

IMPORTANT

Drop a policy - postgres docs

Arguments ​

NameTypeDescription
tableNameNamename of the table where the policy is
policyNamestringname of the policy to delete
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops policy only if it exists

Operation: alterPolicy ​

pgm.alterPolicy( tableName, policyName, options ) ​

IMPORTANT

Alter a policy - postgres docs

Arguments ​

NameTypeDescription
tableNameNamename of the table where the policy is
policyNamestringname of the policy to alter
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
rolestringthe role(s) to which the policy is to be applied
usingstringSQL conditional expression for visibility check
checkstringSQL conditional expression for insert/update check
commentstring or nullComment on the policy; null or '' removes it

To change only a policy's comment, pass only comment. This emits a COMMENT ON POLICY statement and preserves the policy's roles and expressions:

js
pgm.alterPolicy({ schema: 'app', name: 'accounts' }, 'active_accounts', {
  comment: 'Visibility is limited to active accounts',
});

Omitting comment preserves any existing comment. alterPolicy requires an explicit down migration to restore the previous comment or policy options.

Operation: renamePolicy ​

pgm.renamePolicy( tableName, policyName, newPolicyName ) ​

IMPORTANT

Rename a policy - postgres docs

Arguments ​

NameTypeDescription
tableNameNamename of the table where the policy is
policyNamestringold name of the policy
newPolicyNamestringnew name of the policy