gpt4 book ai didi

sql - 如何向用户授予执行 pgcrypto 摘要功能

转载 作者:行者123 更新时间:2023-11-29 12:02:37 24 4
gpt4 key购买 nike

使用 postgresql 9.6

我使用 postgres 用户使用 create extension pgcrypto 启用了 pgcrypto。现在我想授予我的其他数据库用户执行权限。不幸的是我做不到。这可能吗,或者您必须是 super 用户才能使用 pgcrypto 的摘要功能。

postgres=# GRANT EXECUTE ON FUNCTION digest TO another_user;
ERROR: syntax error at or near "digest"
LINE 1: GRANT EXECUTE ON FUNCTION digest TO another_user;

使用下面的答案,我能够成功授予执行该功能的权限。但是 another_user 无法执行该功能。我是否需要其他权限才能使用 another_user 执行此功能?

another_user=> SELECT digest('whatisgoingon'::text, 'sha256'::text);
ERROR: function digest(text, text) does not exist
LINE 1: SELECT digest('whatisgoingon'::text, 'sha256'::text);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

即使当我检查用户的权限时,我会返回我有权限。

postgres=# select has_function_privilege('another_user', 'digest(text, text)', 'execute');
has_function_privilege
------------------------
t
(1 row)

谢谢

最佳答案

Postgres 支持 overloading ,即具有相同名称但参数列表不同的多个函数。

在 SQL 中调用函数时,它会根据参数的数量和类型来判断您指的是哪个版本。但是在 DDL 命令(DROPALTERGRANT 等)中引用该函数时,您需要准确指定您指的是哪个版本,通过在函数名称后包含参数类型列表。

这与 digest 的情况非常相关,因为实际上有 two versions , 你需要弄清楚你说的是哪一个。所以要么:

GRANT EXECUTE ON FUNCTION digest(text,text) TO another_user

...或者:

GRANT EXECUTE ON FUNCTION digest(bytea,text) TO another_user

(...或两者兼而有之。)


从 Postgres 10 开始,您可以在函数未重载时省略参数列表。对于 digest,这对您帮助不大,但至少您会得到一条信息更丰富的错误消息:

postgres=# GRANT EXECUTE ON FUNCTION digest TO another_user;
ERROR: function name "digest" is not unique
HINT: Specify the argument list to select the function unambiguously.

关于您关于 function does not exist 错误的后续问题,请尝试对函数名称进行模式限定,例如SELECT my_schema.digest(...)

  • 如果可行,则为搜索路径问题。您可以继续使用显式架构名称调用它,或者更新您的 search_path .

  • 如果它以 ERROR: permission denied for schema my_schema 响应,那么您只需GRANT USAGE ON SCHEMA my_schema TO another_user

  • 如果它仍然显示 function my_schema.digest(text, text) does not exist,那么您可能错误地连接到了错误的数据库。

关于sql - 如何向用户授予执行 pgcrypto 摘要功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48636396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com