service_role key not working locally on rpc call

I created a custom function to get a user by its id:

CREATE OR REPLACE FUNCTION public.get_user_id_by_email(user_email text)
 RETURNS uuid
 LANGUAGE plpgsql
AS $function$
declare
  user_id uuid;
begin
  select id 
  from auth.users 
  where email = user_email 
  into user_id;

  return user_id;
end;
$function$
;


However, when trying to execute it using the RPC function I got the following error:

{
  code: '42501',
  details: null,
  hint: null,
  message: 'permission denied for table users'
}


Note: I double-checked that I am using the right service_role key by calling any of the auth.admin methods and it worked.
Was this page helpful?