Supabase Row Level Security BlockingYour Queries — Fixed [2026]
Also covers: empty array vs permission error · SELECT/INSERT policies · auth.uid() · service role · anon access
⚡ The Symptoms
// Query returns empty array (not an error) — RLS silently blocking
const { data } = await supabase.from("posts").select("*")
console.log(data) // [] — RLS blocked the query
// OR explicit error for INSERT/UPDATE/DELETE:
{ code: "42501", message: "permission denied for table posts" }✅ Root cause — RLS enabled, no policy added
-- Allow authenticated users to read their own posts
CREATE POLICY "Users can read own posts"
ON posts FOR SELECT
USING (auth.uid() = user_id);How Supabase RLS Works (and Why It Blocks You)
Row Level Security is a PostgreSQL feature that Supabase enables by default on new tables created through the dashboard. When RLS is on, DENY ALL is the default — every query returns zero rows or a permission error unless a policy explicitly allows it.
This is the correct behaviour for security, but it catches developers off guard during initial setup. The fix is always to add the appropriate policies for each operation (SELECT, INSERT, UPDATE, DELETE) you want to permit.
Add a SELECT Policy to Read Rows
Query returns empty array — most common RLS issueAdd a SELECT policy that defines which rows the current user is allowed to read. The USING clause is a SQL expression that filters rows — only rows where this expression is true are returned.
-- Pattern 1: Users can only read their own rows
CREATE POLICY "Users read own rows"
ON posts FOR SELECT
USING (auth.uid() = user_id);
-- Pattern 2: Public read — anyone can read (blog posts, products)
CREATE POLICY "Public can read posts"
ON posts FOR SELECT
USING (true);
-- Pattern 3: Only read published rows
CREATE POLICY "Read published posts"
ON posts FOR SELECT
USING (status = 'published' OR auth.uid() = user_id);
-- Pattern 4: Read rows belonging to user's organisation
CREATE POLICY "Read own org rows"
ON documents FOR SELECT
USING (
organisation_id IN (
SELECT organisation_id FROM memberships WHERE user_id = auth.uid()
)
);Add INSERT, UPDATE, DELETE Policies
Write operations getting permission deniedEach operation needs its own policy. INSERT uses WITH CHECK (validates the new row), UPDATE uses both USING (which rows can be updated) and WITH CHECK (what the updated row must look like), and DELETE uses only USING.
-- SELECT: read own posts
CREATE POLICY "Users read own posts"
ON posts FOR SELECT
USING (auth.uid() = user_id);
-- INSERT: only insert rows where user_id matches the logged-in user
CREATE POLICY "Users insert own posts"
ON posts FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- UPDATE: only update own rows, and user_id must still match after update
CREATE POLICY "Users update own posts"
ON posts FOR UPDATE
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- DELETE: only delete own rows
CREATE POLICY "Users delete own posts"
ON posts FOR DELETE
USING (auth.uid() = user_id);You can combine all operations into one policy with FOR ALL, but separate policies per operation give you finer control and are easier to debug.
Use Service Role Key to Bypass RLS (Server Only)
Admin operations, background jobs, seedingThe service role key bypasses RLS entirely. Use it only in server-side code — never expose it to the browser. Create a separate admin Supabase client using the service role key.
import { createClient } from "@supabase/supabase-js"
// ⚠️ NEVER import this file in Client Components
// ⚠️ NEVER use SUPABASE_SERVICE_ROLE_KEY in the browser
export const supabaseAdmin = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!, // not NEXT_PUBLIC_ — server only
{
auth: {
autoRefreshToken: false,
persistSession: false,
},
}
)import { supabaseAdmin } from "@/lib/supabase/admin"
import { NextResponse } from "next/server"
// This route handler bypasses RLS — use for admin/internal operations only
export async function POST() {
const { data, error } = await supabaseAdmin
.from("posts")
.insert([{ title: "Seed post", user_id: "admin-uuid", status: "published" }])
if (error) return NextResponse.json({ error: error.message }, { status: 500 })
return NextResponse.json({ data })
}SUPABASE_SERVICE_ROLE_KEY must never start with NEXT_PUBLIC_. If it does, it is exposed to the browser and any visitor can bypass your RLS entirely.
Debug RLS with auth.uid() in SQL Editor
Policy not working — test in SQL editorUse the Supabase SQL Editor to test your policies by simulating different user contexts.
-- Check what auth.uid() returns (should be your user's UUID when authenticated)
SELECT auth.uid();
-- Test as anon user (not logged in)
SET LOCAL role = anon;
SELECT * FROM posts; -- should return only publicly allowed rows
-- Check existing policies on a table
SELECT policyname, cmd, qual, with_check
FROM pg_policies
WHERE tablename = 'posts';
-- Check if RLS is enabled on a table
SELECT relname, relrowsecurity
FROM pg_class
WHERE relname = 'posts';
-- Temporarily disable RLS to confirm data exists (dev only, revert after!)
ALTER TABLE posts DISABLE ROW LEVEL SECURITY;
SELECT * FROM posts; -- if rows appear, RLS was blocking — not a data issue
ALTER TABLE posts ENABLE ROW LEVEL SECURITY;Fix RLS for Public Tables (Anon Access)
Blog posts, products, public content — no login requiredFor tables with public content (blog posts, product listings, pricing), add a policy that allows the anon role to read rows without authentication.
-- Allow anyone (logged in or not) to read published posts
CREATE POLICY "Public read published posts"
ON posts FOR SELECT
TO anon, authenticated -- applies to both roles
USING (status = 'published');
-- Allow authenticated users to manage their own posts
CREATE POLICY "Authors manage own posts"
ON posts FOR ALL
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Verify anon role has SELECT permission on the table
GRANT SELECT ON posts TO anon;
GRANT SELECT ON posts TO authenticated;Prevention
- Enable RLS on every table — never leave it disabled in production
- Add policies for every operation you need — SELECT, INSERT, UPDATE, DELETE each need their own
- Use auth.uid() = user_id as the foundation of user-specific policies
- Store SUPABASE_SERVICE_ROLE_KEY without NEXT_PUBLIC_ prefix — server-only, never in browser
- Test policies in the SQL Editor with SET LOCAL role before deploying
- Add GRANT SELECT ON table TO anon for tables that need public (unauthenticated) read access
Frequently Asked Questions
Why does my Supabase query return an empty array instead of data?+−
What is the difference between RLS enabled and RLS disabled?+−
What is auth.uid() in Supabase RLS policies?+−
When should I use the service role key?+−
How do I allow public read access to a Supabase table?+−
Can I test RLS policies without logging in?+−
Need Expert Help?
We Design Secure Supabase RLS Policies
Softplix engineers architect RLS policies, service role patterns, and Supabase security for production apps. Need help securing your database? Let us help.
Talk to an Engineer