SupabaseRLSError Fix2026 Updated

Supabase Row Level Security BlockingYour Queries — Fixed [2026]

Also covers: empty array vs permission error · SELECT/INSERT policies · auth.uid() · service role · anon access

January 20267 min readSupabase · PostgreSQL · RLS Policies

⚡ The Symptoms

Error
// 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

Supabase SQL Editor — add a SELECT policy
-- 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.

1

Add a SELECT Policy to Read Rows

Query returns empty array — most common RLS issue
3 min

Add 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.

Common SELECT policy patterns
-- 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()
    )
  );
2

Add INSERT, UPDATE, DELETE Policies

Write operations getting permission denied
5 min

Each 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.

Full CRUD policies for a posts table
-- 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.

3

Use Service Role Key to Bypass RLS (Server Only)

Admin operations, background jobs, seeding
3 min

The 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.

lib/supabase/admin.ts — server only
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,
    },
  }
)
app/api/admin/seed/route.ts — admin operation
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.

4

Debug RLS with auth.uid() in SQL Editor

Policy not working — test in SQL editor
3 min

Use the Supabase SQL Editor to test your policies by simulating different user contexts.

Supabase SQL Editor — test RLS policies
-- 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;
5

Fix RLS for Public Tables (Anon Access)

Blog posts, products, public content — no login required
2 min

For tables with public content (blog posts, product listings, pricing), add a policy that allows the anon role to read rows without authentication.

Public read access — no login required
-- 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

Frequently Asked Questions

Why does my Supabase query return an empty array instead of data?+
When Row Level Security (RLS) is enabled and no policy allows the current user to read the rows, Supabase silently returns an empty array instead of a permission error. This is intentional — it prevents leaking the existence of data. Add a SELECT policy to allow access to the rows.
What is the difference between RLS enabled and RLS disabled?+
With RLS disabled, anyone with the anon key can read and write all rows in the table — dangerous for production. With RLS enabled, all access is denied by default until you create policies that explicitly allow it. Always enable RLS on tables that contain user data.
What is auth.uid() in Supabase RLS policies?+
auth.uid() returns the UUID of the currently authenticated user. Use it in RLS policies to restrict rows to the owner — for example: auth.uid() = user_id ensures each user can only read their own rows.
When should I use the service role key?+
Use the service role key only in server-side code (Next.js Server Components, API routes, Server Actions, cron jobs) that needs to bypass RLS — for example, admin operations, seeding data, or background jobs. Never expose the service role key to the browser.
How do I allow public read access to a Supabase table?+
Create a SELECT policy with 'true' as the condition: CREATE POLICY allow_public_read ON table_name FOR SELECT USING (true). This allows anyone — authenticated or anonymous — to read the table. Use this for public content like blog posts or product listings.
Can I test RLS policies without logging in?+
Yes. In the Supabase SQL Editor, you can use SET LOCAL role = anon to simulate an anonymous request, or SET LOCAL role = authenticated; SET LOCAL request.jwt.claims = JSON to simulate an authenticated user. This lets you test your policies directly in SQL.

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