🗄️

Row-Level Security Policy Generator

Implement PostgreSQL Row-Level Security for multi-user data isolation

Intermediate🗄️ DatabaseAll Models
prompt template
Set up Row-Level Security (RLS) for:

**Tables:** [TABLES — e.g., "projects, tasks, comments, files"]
**Roles:** [ROLES — e.g., "admin (full access), manager (team access), member (own data only)"]
**Tenant model:** [MODEL — e.g., "organization_id on every table", "team-based access"]

Generate:
1. **RLS policies** — SELECT, INSERT, UPDATE, DELETE policies for each table and role
2. **Current user context** — set session variable with authenticated user info
3. **Policy functions** — reusable security functions for common patterns
4. **Cascading access** — manager sees team data, admin sees everything
5. **Performance** — ensure policies use indexed columns, avoid sequential scans
6. **Testing** — queries to verify each role sees only authorized data
7. **Bypass** — service role for migrations and admin operations

Include the complete SQL for enabling RLS, creating policies, and test queries.

How to Use This Prompt

  1. 1Copy the prompt template above
  2. 2Paste into Claude, ChatGPT, or Cursor
  3. 3Replace [bracketed placeholders] with your specific project details
  4. 4Iterate on the AI output to refine and customize the results