postgresql 9.1 - 通过函数访问表

发布于 2025-01-04 23:05:05 字数 308 浏览 0 评论 0原文

我有 3 个角色:超级用户、高级用户和用户。我有表“数据”和函数 data_select 和 data_insert。

现在我想定义,只有超级用户才能访问表“数据”。 Poweruser和user不能直接访问表“数据”,只能通过函数访问。

用户只能运行data_select函数,高级用户可以运行data_select和data_insert。

这样我就可以创建用户 alice、bob……并继承他们的 user 或 poweruser 权限。

这实际上可以实现吗?我已经为此奋斗了第二天,却一事无成。

谢谢您的宝贵时间。

I have 3 roles: superuser, poweruser and user. I have table "data" and functions data_select and data_insert.

Now I would like to define, that only superuser can access table "data". Poweruser and user can not access table "data" directly, but only through the functions.

User can run only function data_select, poweruser can run both data_select and data_insert.

So then I can create users alice, bob, ... and inherits them privileges of user or poweuser.

Is this actually achievable? I am fighting with this for the second day and not getting anywhere.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-01-11 23:05:05

是的,这是可行的。

默认情况下,“超级用户”可以是实际的超级用户postgres
我将普通用户的角色重命名为 usr,因为 user 是保留字 - 不要将其用作标识符。

CREATE ROLE usr;
CREATE ROLE poweruser;
GRANT usr TO poweruser;  -- poweruser can do everything usr can.

CREATE ROLE bob PASSWORD <password>;
GRANT poweruser TO bob;

CREATE ROLE alice PASSWORD <password>;
GRANT usr TO alice;

REVOKE ALL ON SCHEMA x FROM public;
GRANT USAGE ON SCHEMA x TO usr;

REVOKE ALL ON TABLE x FROM public;
REVOKE ALL ON TABLE y FROM public;

CREATE FUNCTION
  ...
SECURITY DEFINER;

REVOKE ALL ON FUNCTION ... FROM public;
GRANT EXECUTE ON FUNCTION a TO usr;
GRANT EXECUTE ON FUNCTION b TO poweruser;

或者,您可以创建无需登录的守护程序角色来拥有这些功能并持有表上的相应权限。这样就更安全了。

如果您选择这条路线,您会喜欢 更改默认权限(随 PostgreSQL 9.0 引入)。 此相关答案中有更多详细信息

阅读章节编写SECURITY DEFINER< /code> 手册中的安全功能

Yes, this is doable.

"superuser" could be an actual superuser, postgres by default.
I rename the role for plain users to usr, because user is a reserved word - don't use it as identifier.

CREATE ROLE usr;
CREATE ROLE poweruser;
GRANT usr TO poweruser;  -- poweruser can do everything usr can.

CREATE ROLE bob PASSWORD <password>;
GRANT poweruser TO bob;

CREATE ROLE alice PASSWORD <password>;
GRANT usr TO alice;

REVOKE ALL ON SCHEMA x FROM public;
GRANT USAGE ON SCHEMA x TO usr;

REVOKE ALL ON TABLE x FROM public;
REVOKE ALL ON TABLE y FROM public;

CREATE FUNCTION
  ...
SECURITY DEFINER;

REVOKE ALL ON FUNCTION ... FROM public;
GRANT EXECUTE ON FUNCTION a TO usr;
GRANT EXECUTE ON FUNCTION b TO poweruser;

Or you could create daemon roles with no login to own the functions and hold the respective rights on the table. That would be even more secure.

If you are going this route, you will love ALTER DEFAULT PRIVILEGES (introduced with PostgreSQL 9.0). More details in this related answer.

Read the chapter Writing SECURITY DEFINER Functions Safely in the manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文