Policies Operations
Operation: createPolicy
pgm.createPolicy( tableName, policyName, options )
IMPORTANT
Create a new policy - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
tableName | Name | name of the table to alter |
policyName | string | name of the new policy |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
as | string | PERMISSIVE (default) or RESTRICTIVE |
command | string | ALL, SELECT, INSERT, UPDATE, or DELETE |
role | string or array | the role(s) to which the policy is to be applied |
using | string | SQL conditional expression for visibility check |
check | string | SQL conditional expression for insert/update check |
comment | string or null | Comment 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.
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
| Name | Type | Description |
|---|---|---|
tableName | Name | name of the table where the policy is |
policyName | string | name of the policy to delete |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
ifExists | boolean | drops policy only if it exists |
Operation: alterPolicy
pgm.alterPolicy( tableName, policyName, options )
IMPORTANT
Alter a policy - postgres docs
Arguments
| Name | Type | Description |
|---|---|---|
tableName | Name | name of the table where the policy is |
policyName | string | name of the policy to alter |
options | object | Check below for available options |
Options
| Option | Type | Description |
|---|---|---|
role | string | the role(s) to which the policy is to be applied |
using | string | SQL conditional expression for visibility check |
check | string | SQL conditional expression for insert/update check |
comment | string or null | Comment 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:
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
| Name | Type | Description |
|---|---|---|
tableName | Name | name of the table where the policy is |
policyName | string | old name of the policy |
newPolicyName | string | new name of the policy |