Deutsch
Error messageSupabase · Functions

Fix “permission denied for function” in Supabase (error 42501)

Why supabase.rpc() or a policy fails with permission denied for function, the four usual causes, and how to grant execute without opening admin functions.

The message you seePostgres · 42501

permission denied for function get_team_stats

By Tim GeithnerPublished 5 min read

Short answer

Postgres refused to run a function because the role behind the request has no execute privilege on it. Usually a hardening step revoked execute and nobody granted it back to the role that needs it, the request runs signed out, the client calls a function that is meant for the server, or a policy uses a helper function the user may not run. Grant execute only to the role that needs it. Never grant execute on all functions to anon, and give every security definer function its own caller check and an empty search_path.

Ask an AI about this page:ChatGPTClaude

On this page

To run a function, the role behind the request needs the execute privilege on it. Supabase requests run as anon without a session, authenticated with one, or service_role with the secret key. If that role has no execute on the function, Postgres refuses with code 42501 and this message, and the function does not run at all. It happens on supabase.rpc(), but also on a plain select when a policy on the table calls a function.

Unlike tables, new functions are not affected by Supabase’s 2026 change to new tables. Supabase’s docs say new functions are still callable by every role unless you change the defaults. So when this error appears, someone took execute away, on purpose or not.

Find your cause in 30 seconds

What you seeMost likely cause
It broke after a security fix or a Security Advisor cleanupExecute was revoked
It works when signed in, fails on a public page or right after loadThe request is signed out
The function adds credits, changes plans or sends mailThe function is meant for the server
A select fails and the message names a helper like is_memberA policy uses a function the user may not run

We ran every cause on Postgres with Supabase’s roles. The function on this page counts a team’s projects and already checks that the caller is a member of that team:

The tables and function used on this page
create table public.members (
  team_id uuid not null,
  user_id uuid not null references auth.users (id) on delete cascade,
  primary key (team_id, user_id)
);

create table public.projects (
  id uuid primary key default gen_random_uuid(),
  team_id uuid not null,
  name text not null
);

alter table public.members enable row level security;
alter table public.projects enable row level security;

create or replace function public.get_team_stats(_team uuid)
returns bigint
language plpgsql
stable
security definer
set search_path = ''
as $$
begin
  if not exists (
    select 1 from public.members m
    where m.team_id = _team
      and m.user_id = (select auth.uid())
  ) then
    raise exception 'not a member of this team';
  end if;

  return (select count(*) from public.projects p where p.team_id = _team);
end;
$$;

The app calls it like this:

The call in your app
const { data, error } = await supabase.rpc("get_team_stats", { _team: teamId });

The four causes

1. Execute was revoked

The Security Advisor warns about security definer functions that visitors or signed-in users can call. The quick answer an AI builder writes is to revoke execute from everyone, often copied from Supabase’s docs:

A hardening migration that blocks every function
revoke execute on all functions in schema public from public;
revoke execute on all functions in schema public from anon, authenticated;
alter default privileges revoke execute on functions from public;
alter default privileges in schema public revoke execute on functions from anon, authenticated;

That is a good start, but only half the job. The first two lines block every existing function, including the ones your app calls. The last two make every function created afterwards start with no execute for the API roles. Each function the app needs must then be granted back, one by one.

2. The request is signed out

If only authenticated may run the function, a call that happens before supabase-js has restored the session runs as anon and fails. That is correct behaviour. Wait for the session (supabase.auth.getSession() or the auth state listener) before you call the function, instead of granting it to anon.

3. The function is meant for the server

Functions that add credits, change a plan or touch other users’ data should never be called from the browser. If the client gets this error for such a function, the grants are right and the call is in the wrong place. Move it into an Edge Function or webhook that checks the request and calls the function with the secret key:

SafeA server-only function stays server-only
revoke execute on function public.add_credits(uuid, integer) from public, anon, authenticated;
grant execute on function public.add_credits(uuid, integer) to service_role;

4. A policy uses a function the user may not run

Team apps often check membership in policies with a helper function. If a migration revoked execute on the helper from public and never granted it to authenticated, every query on the protected table fails, and the message names the helper instead of your table:

VulnerableA policy helper that nobody may run
create schema if not exists private;

create or replace function private.is_member(_team uuid)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
  select exists (
    select 1 from public.members m
    where m.team_id = _team
      and m.user_id = (select auth.uid())
  );
$$;

revoke all on function private.is_member(uuid) from public;

create policy "Members can see their team's projects"
  on public.projects for select
  to authenticated
  using (private.is_member(team_id));

How to fix it

Grant execute per function, to the role that really calls it, and take it away from everyone else. For the function the app calls when signed in:

SafeFix: execute only for the role that calls it
revoke execute on function public.get_team_stats(uuid) from public, anon;
grant execute on function public.get_team_stats(uuid) to authenticated;

For a policy helper, the role that runs the query needs execute, and nobody else:

SafeFix: let signed-in users run the policy helper
grant execute on function private.is_member(uuid) to authenticated;

Before you grant execute on a security definer function, check two things inside it. It must check the caller itself, like the membership check in get_team_stats, because it runs with its owner’s rights and row level security does not protect anything it reads. And it must have set search_path = '' with every table written as public.name. The guide to security definer functions explains both, and the fix for definer functions without checks shows how to repair one that trusts its arguments.

VulnerableThe fix that opens every function
grant execute on all functions in schema public to anon, authenticated;
Fix permission denied for function without opening admin functions

My Supabase app fails with “permission denied for function your_function” (code 42501). Find the function in the migrations, every migration that grants or revokes execute on it, and every place that calls it: supabase.rpc() in the client, Edge Functions, and policies that use it. Then tell me which role each caller runs as (anon, authenticated or service_role).

Fix it in a new migration: revoke execute on that function from public and anon, and grant execute only to the role that needs it. If the client calls a function that changes credits, plans, payments or other users’ data, do not grant it to authenticated; move the call into an Edge Function that checks the request and uses the service role. If the function is security definer, make sure it checks the caller inside with (select auth.uid()) and has set search_path = ‘’ with schema-qualified names.

Do not grant execute on all functions, do not grant anything to anon that is not needed, do not use the service role key in client code and do not edit existing migrations. Show me the migration before applying it.

Check that the fix works

In the SQL Editor, list who may call each function. It needs no user id:

Check: who may call your functions
select p.oid::regprocedure as function,
       p.prosecdef as security_definer,
       has_function_privilege('anon', p.oid, 'execute') as visitors_can_call,
       has_function_privilege('authenticated', p.oid, 'execute') as users_can_call
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('public', 'private')
order by 1;

get_team_stats and is_member should show true only under users_can_call, and server-only functions like add_credits false in both columns. Then call the function as a member and as someone outside the team:

Check: call as a user
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
select public.get_team_stats('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
rollback;

A member gets the count. A user from another team gets not a member of this team, which is the function’s own check doing its job. The guide to testing RLS policies shows the same pattern for tables.

Frequently asked questions

Did the 2026 Supabase change to new tables cause this?

Not directly. That change stops new tables from being granted to the API roles. Supabase’s docs say new functions are still callable by every role unless you change the defaults, so this error almost always means someone revoked execute.

Should I grant execute on all functions so the app works again?

No. Some functions are meant only for your server, such as adding credits after a payment. Granting all of them to anon or authenticated lets any visitor call those directly with supabase.rpc(). Grant execute per function, to the role that needs it.

Why does my select fail with permission denied for function when I never call a function?

A policy on the table calls one. The user needs execute on every function a policy uses, such as a membership helper. Grant execute on that helper to authenticated, and only to authenticated.

Is it safe to grant execute on a security definer function to authenticated?

Only if the function checks inside who is calling, for example by comparing (select auth.uid()) with a membership table, and has set search_path = ‘’. A definer function runs with its owner’s rights and skips RLS, so its own check is the only protection.

Sources

  1. 1.Database functions — Supabase Docs
  2. 2.Securing your API — Supabase Docs
  3. 3.CREATE FUNCTION: Writing SECURITY DEFINER Functions Safely — PostgreSQL Documentation
  4. 4.Privileges — PostgreSQL Documentation
  5. 5.Database API 42501 errors — Supabase Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt