【问题标题】:Supabase client permission denied for schema public架构公共的 Supabase 客户端权限被拒绝
【发布时间】:2021-08-05 15:32:12
【问题描述】:

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

error: {
    hint: null,
    details: null,
    code: '42501',
    message: 'permission denied for schema public'
}

我认为这与我用来处理迁移的 Prisma 有关。当我只是点击原型时,客户端运行良好,但设置 Prisma 后,它不再运行了。

关于如何解决此问题的任何建议?我真的很希望能够同时使用 Supabase REST API 和 Prisma。

【问题讨论】:

  • 我也遇到了同样的错误,也使用了 Prisma 和 Supabase。你能解决问题吗?

标签: postgresql prisma prisma-graphql supabase


【解决方案1】:

原因:当你在远程 Supbase 数据库中执行 prisma migrate reset 或 prisma migrate dev 时,你可能需要重置它。重置数据库会删除整个数据库并重新创建它。 Source

这将清除 PostgREST 工作所需的所有 GRANTs 您可以在 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'

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

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;

这里是示例表members的默认权限列表

grantor grantee table_catalog table_schema table_name privilege_type is_grantable with_hierarchy
postgres postgres postgres public members INSERT YES NO
postgres postgres postgres public members SELECT YES YES
postgres postgres postgres public members UPDATE YES NO
postgres postgres postgres public members DELETE YES NO
postgres postgres postgres public members TRUNCATE YES NO
postgres postgres postgres public members REFERENCES YES NO
postgres postgres postgres public members TRIGGER YES NO
postgres anon postgres public members INSERT NO NO
postgres anon postgres public members SELECT NO YES
postgres anon postgres public members UPDATE NO NO
postgres anon postgres public members DELETE NO NO
postgres anon postgres public members TRUNCATE NO NO
postgres anon postgres public members REFERENCES NO NO
postgres anon postgres public members TRIGGER NO NO
postgres authenticated postgres public members INSERT NO NO
postgres authenticated postgres public members SELECT NO YES
postgres authenticated postgres public members UPDATE NO NO
postgres authenticated postgres public members DELETE NO NO
postgres authenticated postgres public members TRUNCATE NO NO
postgres authenticated postgres public members REFERENCES NO NO
postgres authenticated postgres public members TRIGGER NO NO
postgres service_role postgres public members INSERT NO NO
postgres service_role postgres public members SELECT NO YES
postgres service_role postgres public members UPDATE NO NO
postgres service_role postgres public members DELETE NO NO
postgres service_role postgres public members TRUNCATE NO NO
postgres service_role postgres public members REFERENCES NO NO
postgres service_role postgres public members TRIGGER NO NO

【讨论】:

    【解决方案2】:

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

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

    似乎 Supabase 项目架构中有一堆特定于 Supabase 的配置,如果你把它弄乱了 - 就会发生奇怪的事情。

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

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我只是在 Supabase 上创建了一个全新的项目,然后“npx prisma migrate dev”重新生成我的表,它就像一个魅力!我不知道我什么时候破坏了我的 Supabase 项目。如果您因为有数据而无法重新生成表,请考虑将数据迁移到新项目,因为它确实有效!

      【讨论】:

        猜你喜欢
        • 2023-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-24
        • 2018-03-11
        相关资源
        最近更新 更多