Syntax
CREATE POLICY
features:
- Policy names: Unique identifiers for Row Level Security policies
- Table references: Schema-qualified table names where policies apply
- Policy type:
- PERMISSIVE (default) - allows access when policy evaluates to true
- RESTRICTIVE - denies access unless policy evaluates to true
- Commands:
- ALL - applies to all commands
- SELECT - applies to SELECT queries
- INSERT - applies to INSERT commands
- UPDATE - applies to UPDATE commands
- DELETE - applies to DELETE commands
- Roles: Specific roles, PUBLIC, CURRENT_USER, or SESSION_USER that the policy applies to
- USING clause: Boolean expression checked for existing rows (SELECT, UPDATE, DELETE)
- WITH CHECK clause: Boolean expression checked for new rows (INSERT, UPDATE)
Canonical Format
When generating migration SQL, pgschema produces policies in the following canonical format:- Schema qualification is omitted for tables in the target schema
- The AS PERMISSIVE/RESTRICTIVE clause is omitted when PERMISSIVE (the default)
- FOR ALL is omitted as it’s the default command
- Roles are listed in a consistent order
- USING and WITH CHECK expressions are preserved exactly as defined
- For ALTER operations that only change roles, USING, or WITH CHECK:
- For changes requiring recreation (command or permissive/restrictive changes):
Row Level Security
Policies require Row Level Security to be enabled on the table:ALTER TABLE ... ENABLE/DISABLE ROW LEVEL SECURITY
statements in the migration plan.