加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
best_practices.rst 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
H20-17 提交于 2020-02-18 17:04 . Clarify function privileges section (#303)

Function privileges

By default, when a function is created, the right to execute it is is not restricted by role, but this probably isn't consistent with best practices for an API design. If you want functions to be executable exclusively by a given role upon their creation, issue psql instructions similar to this:

-- To stop functions from being universally executable upon creation (note the IN SCHEMA part).
ALTER DEFAULT PRIVILEGES IN SCHEMA api REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
-- To grant execution rights for functions to a specific role upon function creation.
ALTER DEFAULT PRIVILEGES IN SCHEMA api GRANT EXECUTE ON FUNCTIONS TO my_role;

See PostgreSQL alter default privileges for more details.

The foregoing example may not be appropriate in all situations. For instance you may have a situation where different functions are intended to be called by different roles. In that case you will not want to grant EXECUTE to one specific role by default. Instead you will want to manually grant executability on a case by case basis.

By default, a function is executed with the privileges of the user who calls it. This means that the user has to have all permissions to do the operations the procedure performs.

Another option is to define the function with the SECURITY DEFINER option. Then only one permission check will take place, the permission to call the function, and the operations in the function will have the authority of the user who owns the function itself. See PostgreSQL documentation for more details.

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化