Supabase 客户端权限被拒绝,模式为 public

rda*_*vis 15 postgresql prisma prisma-graphql supabase

每当我尝试使用 @supabase/supabase-js 查询数据库时,都会收到错误。

error: {
    hint: null,
    details: null,
    code: '42501',
    message: 'permission denied for schema public'
}
Run Code Online (Sandbox Code Playgroud)

我认为这与我用来处理迁移的 Prisma 有关。当我只是浏览原型时,客户端工作得很好,但设置 Prisma 后,它就不再工作了。

对于如何解决这个问题,有任何的建议吗?我真的很希望能够同时使用 Supabase REST API 和 Prisma。

kac*_*har 19

原因:当您执行prisma migrate resetprisma migrate dev在远程 Supabase 数据库中时,您可能需要重置它。重置数据库会删除整个数据库并重新创建它。来源

这将清除GRANTPostgREST 工作所需的所有内容您可以在 PostgREST 文档中找到该错误:

https://postgrest.org/en/latest/tutorials/tut0.html?highlight=42501#step-5-run-postgrest

解决方案 1:创建一个全新的项目并仅prisma migrate deploy在远程数据库上使用。

解决方案 2:通过授予对表的访问权限来手动修复授权

您可以在一个全新的 Supabase 项目和损坏的数据库上运行以下 SQL 来检查差异。

SELECT *
FROM information_schema.role_table_grants 
WHERE table_schema='public' and table_name='members'
Run Code Online (Sandbox Code Playgroud)

以下默认授权取自supabase github存储库,网址为https://github.com/supabase/supabase/blob/a2fc6d592cb4ea50fd518b99db199a31912040b9/docker/volumes/db/init/00-initial-schema.sql#L26-L29

grant usage on schema public to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role;

alter default privileges for user supabase_admin in schema public grant all
    on sequences to postgres, anon, authenticated, service_role;
alter default privileges for user supabase_admin in schema public grant all
    on tables to postgres, anon, authenticated, service_role;
alter default privileges for user supabase_admin in schema public grant all
    on functions to postgres, anon, authenticated, service_role;
Run Code Online (Sandbox Code Playgroud)

以下是示例表的默认权限列表members

授予人 受赠者 表目录 表模式 表名 特权类型 是_grantable with_hierarchy
postgres postgres postgres 民众 会员 插入 是的
postgres postgres postgres 民众 会员 选择 是的 是的
postgres postgres postgres 民众 会员 更新 是的
postgres postgres postgres 民众 会员 删除 是的
postgres postgres postgres 民众 会员 截短 是的
postgres postgres postgres 民众 会员 参考 是的
postgres postgres postgres 民众 会员 扳机 是的
postgres 匿名 postgres 民众 会员 插入
postgres 匿名 postgres 民众 会员 选择 是的
postgres 匿名 postgres 民众 会员 更新
postgres 匿名 postgres 民众 会员 删除
postgres 匿名 postgres 民众 会员 截短
postgres 匿名 postgres 民众 会员 参考
postgres 匿名 postgres 民众 会员 扳机
postgres 经过验证的 postgres 民众 会员 插入
postgres 经过验证的 postgres 民众 会员 选择 是的
postgres 经过验证的 postgres 民众 会员 更新
postgres 经过验证的 postgres 民众 会员 删除
postgres 经过验证的 postgres 民众 会员 截短
postgres 经过验证的 postgres 民众 会员 参考
postgres 经过验证的 postgres 民众 会员 扳机
postgres 服务角色 postgres 民众 会员 插入
postgres 服务角色 postgres 民众 会员 选择 是的
postgres 服务角色 postgres 民众 会员 更新
postgres 服务角色 postgres 民众 会员 删除
postgres 服务角色 postgres 民众 会员 截短
postgres 服务角色 postgres 民众 会员 参考
postgres 服务角色 postgres 民众 会员 扳机


Jos*_*ner 14

请不要只对每种类型的可能访问数据库的用户运行“授予所有权限”。

所需的访问权限取决于 sql 正在执行的操作。即,如果只需要选择和插入,则仅提供这些权限,涉及是否允许每个人访问模式中的截断和删除表...

我在使用supabase.rpc() 运行一些postgres 函数时遇到问题。该函数将从表中选择一些数据,更新表,然后插入到新表中。

我遇到的第一个错误是

{
 code: '42501',
  details: null,
  hint: null,
  message: 'permission denied for schema public'
}
Run Code Online (Sandbox Code Playgroud)

已解决

grant usage on schema "public" to anon;
grant usage on schema "public" to authenticated;
Run Code Online (Sandbox Code Playgroud)

然后导致这个错误

   {
  code: '42501',
 details: null,
  hint: null,
  message: 'permission denied for table xxx'
 }
Run Code Online (Sandbox Code Playgroud)

这是用哪个解决的

GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO authenticated;
GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA "public" TO anon;
Run Code Online (Sandbox Code Playgroud)

请参阅下面的一些资料(因为这是九年级英语课教我做的)

Github问题

有关补助金的文件

  • 这应该是公认的答案。其他答案是让用户向所有用户授予所有权限(包括可能被泄露并被任何人用来访问您的数据库的匿名/公钥) (2认同)

小智 13

在 Supabase SQL 编辑器中运行此 SQL 脚本。

来源:https://supabase.com/docs/guides/integrations/prisma#troubleshooting

CREATE SCHEMA IF NOT EXISTS "auth";
CREATE SCHEMA IF NOT EXISTS "extensions";
create extension if not exists "uuid-ossp"      with schema extensions;
create extension if not exists pgcrypto         with schema extensions;
create extension if not exists pgjwt            with schema extensions;

grant usage on schema public to postgres, anon, authenticated, service_role;
grant usage on schema extensions to postgres, anon, authenticated, service_role;
alter user supabase_admin SET search_path TO public, extensions; -- don't include the "auth" schema

grant all privileges on all tables in schema public to postgres, anon, authenticated, service_role, supabase_admin;
grant all privileges on all functions in schema public to postgres, anon, authenticated, service_role, supabase_admin;
grant all privileges on all sequences in schema public to postgres, anon, authenticated, service_role, supabase_admin;

alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role;
alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role;

alter default privileges for user supabase_admin in schema public grant all on sequences to postgres, anon, authenticated, service_role;
alter default privileges for user supabase_admin in schema public grant all on tables to postgres, anon, authenticated, service_role;
alter default privileges for user supabase_admin in schema public grant all on functions to postgres, anon, authenticated, service_role;

alter role anon set statement_timeout = '3s';
alter role authenticated set statement_timeout = '8s';
Run Code Online (Sandbox Code Playgroud)


Sho*_*orn 1

我之前曾使用 Flyway“清理”功能(删除架构中的所有内容,以便您可以再次运行迁移)来破坏我的 Supabase 项目。

您可以在这里看到它的讨论:https://github.com/supabase/supabase/discussions/344#discussioncomment-182886

看起来 Supabase 项目模式中有一堆 Supabase 特定的配置,如果你搞乱它 - 就会发生奇怪的事情。

您可能想要调查 Prisma 是否对架构做了任何您没有意识到的事情。