如何向用户授予对pgcrypto摘要功能的执行权限

时间:2018-02-06 05:59:53

标签: sql postgresql grant pgcrypto

使用postgresql 9.6

我使用postgres用户使用create extension pgcrypto启用了pgcrypto。 现在我想向我的其他db用户授予执行权限。不幸的是我无法做到这一点。这是可能的,还是你必须是超级用户才能使用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)

由于

1 个答案:

答案 0 :(得分:3)

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

...或:

digest

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

从Postgres 10开始,当函数没有超载时,你可以省略参数列表。在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(...)错误的后续问题,请尝试使用模式限定函数名称,例如: ERROR: permission denied for schema my_schema

  • 如果可以,那就是搜索路径问题。您可以使用明确的架构名称继续调用它,也可以更新search_path

  • 如果它以GRANT USAGE ON SCHEMA my_schema TO another_user回复,那么您只需要function my_schema.digest(text, text) does not exist

  • 如果它仍然显示import React from 'react'; import { render } from 'react-dom'; // import componentns import Main from './components/Main'; import PhotoGrid from './components/PhotoGrid'; import Single from './components/Single'; // import react router import { Router, Route, IndexRoute, BrowserRouter, browserHistory} from 'react-router-dom' class MainComponent extends React.Component { render() { return ( <div> <BrowserRouter history={browserHistory}> <Route path="/" component={Main} > <IndexRoute component={PhotoGrid}></IndexRoute> <Route path="/view/:postId" component={Single}></Route> </Route> </BrowserRouter> </div> ); } } render(<MainComponent />, document.getElementById('root')); ,那么您可能错误地连接到了错误的数据库。