Skip to main content

Syntax

pgschema understands the following 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:
Key characteristics of the 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:
pgschema automatically handles RLS enablement when policies are present and generates the appropriate statements in the migration plan:
  • ALTER TABLE ... ENABLE ROW LEVEL SECURITY - enables RLS for the table
  • ALTER TABLE ... DISABLE ROW LEVEL SECURITY - disables RLS
  • ALTER TABLE ... FORCE ROW LEVEL SECURITY - forces RLS to apply even to table owners
  • ALTER TABLE ... NO FORCE ROW LEVEL SECURITY - removes the force option