c/backend › command
0votes

Model a Postgres schema with row-level security

Prompt Tested
submitted by CommandVault editors to c/backend · 1 use

Tables, indexes and RLS policies for a multi-user feature.

Prompt · 2 variables
I'm building {{feature}} on Postgres (Supabase). Users: {{user_roles}}.

Produce a single SQL migration that:
- Creates tables with uuid primary keys, timestamps, foreign keys and check constraints.
- Adds indexes for every foreign key and every column I'll filter or sort by.
- Enables RLS on every table and writes select/insert/update/delete policies per role, using (select auth.uid()).
- Keeps privileged logic in security definer functions with search_path set to ''.

After the SQL, explain each policy in one sentence and list queries I should run to prove a user cannot read another user's rows.
0 commentsreport

Guide

What you get: One SQL migration with uuid keys, timestamps, foreign keys, checks, indexes for every FK and filter, RLS policies per role using (select auth.uid()), privileged logic in security definer functions with search_path '', plus one-line policy explanations and isolation queries. Best for Supabase/Postgres developers designing a multi-user feature..

Before you start

  • Describe the feature and every role with what it may do (fill feature and user_roles)
  • A local Supabase or Postgres to apply the migration and run the isolation queries

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

feature=shared shopping lists (owner can invite members); user_roles=owner (full control, invites/removes members, deletes the list), invited members (read the list, add/tick off/delete items, not delete the list or manage members), every other signed-in user (no access).

Result

Claude Code: 3 tables, private.has_list_access()/is_list_owner() security definer helpers, an invite_list_member RPC, 11 policies. Codex: 3 tables, private.user_owned_list_ids()/user_member_list_ids() helpers, 18 policies, a trigger limiting members to changing completion state.

We applied each migration to Postgres 16 with a Supabase-like stub and ran 23 checks per schema as owner, member, outsider and anon: all passed for both (members read and tick items but can't rename or delete the list or invite; the outsider sees and changes nothing; anon is denied; removing a member revokes access; deleting the list cascades). No security definer function lacks search_path, no FK lacks an index, every table has RLS. Claude Code wrote the migration to supabase/migrations/; Codex returned it in the reply.

How to check it worked

  • Apply the migration locally and run the isolation queries as two real users
  • Check every security definer function has search_path set
  • Check members can't do the owner-only actions

Common problems

  • Policies that query the same table recurse; both tools used security definer helpers to avoid it
  • A role described vaguely gets guessed permissions; spell out what each role may do

Try it here

Fill in {{feature}}, {{user_roles}} above first for a better answer, or run it as is and the AI will assume sensible values.

or open in ChatGPT · Claude · Perplexity

How to use this prompt

  1. Fill in the 2 blanks above. The more specific you are, the better the answer.
  2. Copy it and paste it into ChatGPT, Claude, Gemini, Cursor or Codex, or use "Try it here" for a quick test.
  3. Read the answer critically: check anything it claims about your code or data before you rely on it.
  4. Careful: this one is marked medium risk. Review what the AI suggests before you run it on real systems or data.
  • {{feature}} Feature
  • {{user_roles}} User roles

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.