© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago•
51 replies
gduteaud

RLS policy help

Hi everyone!
I'm trying to write RLS policies to only allow access to rows of a "vendors" table that are identified in a "vendor_communities" table as belonging to the same community as the active user as determined by the "user_communities" table. Here's what I have:

-- Create a policy for SELECT access on the "vendors" table
CREATE POLICY "Enable select for community members only"
ON "public"."vendors"
AS PERMISSIVE FOR SELECT
    USING (
        EXISTS (
            SELECT 1
            FROM vendor_communities vc
            JOIN user_communities uc ON vc.community = uc.community
            WHERE vc.vendor = vendors.id
            AND uc."user" = auth.uid()
        )
    );

-- Create a policy for INSERT access on the "vendors" table
CREATE POLICY "Enable insert for community members only"
 ON "public"."vendors"
AS PERMISSIVE FOR INSERT
    WITH CHECK (
        EXISTS (
            SELECT 1
            FROM vendor_communities vc
            JOIN user_communities uc ON vc.community = uc.community
            WHERE vc.vendor = vendors.id
            AND uc."user" = auth.uid()
        )
    );
-- Create a policy for SELECT access on the "vendors" table
CREATE POLICY "Enable select for community members only"
ON "public"."vendors"
AS PERMISSIVE FOR SELECT
    USING (
        EXISTS (
            SELECT 1
            FROM vendor_communities vc
            JOIN user_communities uc ON vc.community = uc.community
            WHERE vc.vendor = vendors.id
            AND uc."user" = auth.uid()
        )
    );

-- Create a policy for INSERT access on the "vendors" table
CREATE POLICY "Enable insert for community members only"
 ON "public"."vendors"
AS PERMISSIVE FOR INSERT
    WITH CHECK (
        EXISTS (
            SELECT 1
            FROM vendor_communities vc
            JOIN user_communities uc ON vc.community = uc.community
            WHERE vc.vendor = vendors.id
            AND uc."user" = auth.uid()
        )
    );


These are not working as expected in that they don't grant access when the desired conditions are met. Thanks to Supabase's new user impersonator feature I can confirm that this is a problem with the policies and not something on the front end.

I have another table with similar requirements but an added user role check that's also not working as expected, I suspect for the same reason. Any help would be appreciated!

Thanks,

GD
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

RLS Policy
SupabaseSSupabase / help-and-questions
4mo ago
need help with RLS policy
SupabaseSSupabase / help-and-questions
5mo ago
RLS - Help with Insert Policy
SupabaseSSupabase / help-and-questions
4y ago
Restrictive RLS policy
SupabaseSSupabase / help-and-questions
2mo ago
Next page