简体   繁体   English

防止角色在 postgresql 上公开创建表

[英]Prevent roles from creating tables in public on postgresql

How do you prevent a ROLE from creating TABLES in the PUBLIC schema?如何防止 ROLE 在 PUBLIC 模式中创建 TABLES?

Here is what I have tried:这是我尝试过的:

# Logging in as administrator
PGPASSWORD=my_admin_password psql -h XXX.XXX.XXX.XXX --port XXXXX -d database -U admin_user

# Creating role/user
CREATE ROLE test_role;
GRANT CONNECT ON DATABASE database TO test_role;
CREATE USER test_user WITH ENCRYPTED PASSWORD 'password';
GRANT test_role TO test_user;

# Removing all permissions on public
REVOKE ALL ON DATABASE database FROM PUBLIC;
REVOKE ALL ON ALL TABLES IN SCHEMA public FROM test_role;

Now let's log in with the test_user:现在让我们使用 test_user 登录:

# Logging in as test_user
PGPASSWORD=password psql -h XXX.XXX.XXX.XXX --port XXXXX -d database -U test_user

# Creating a table 
CREATE TABLE test (id int, name text);

Table has been created in public successfully!表已成功创建公开! Wonderful!精彩的! What is going wrong here?这里出了什么问题? Why can a user will all permissions revoked still create tables?为什么一个用户将所有权限都撤销了仍然可以创建表?

Update:更新:

As suggested I have written a few more revokes as the administrator, and it says there is nothing left to revoke.正如建议的那样,我作为管理员已经写了一些撤销,它说没有什么可以撤销的。 STILL, I can create all the tables I want as test_user .仍然,我可以创建我想要的所有表test_user I am starting to speculate if it could be some strange behaviour by the Managed Postgres , where I do not have access to the REAL admin, and it says the owner of public is _rdb_superadmin .我开始推测这是否可能是Managed Postgres的一些奇怪行为,我无法访问REAL管理员,并且它说public的所有者是_rdb_superadmin The Postgresql message is not very helpfull, as it doesn't tell me if all permissions have been revoked or not. Postgresql 消息不是很有帮助,因为它没有告诉我是否所有权限都已被撤销。

在此处输入图像描述

Your command REVOKE ALL ON ALL TABLES IN SCHEMA public only revoked privileges on existing tables in that schema, not any privileges on the schema itself.您的命令REVOKE ALL ON ALL TABLES IN SCHEMA public仅撤销了对该模式中现有表的权限,而不是模式本身的任何权限。

You need to revoke the privileges on the public schema itself as well:您还需要撤销公共架构本身的权限:

revoke all on schema public from public;

It's a bit unclear if you only want to prevent the creation of tables.如果您只想阻止创建表,这有点不清楚。 So maybe you just want所以也许你只是想要

revoke create on schema public from public;

which would keep the usage privilege.这将保留usage权限。


In the upcoming Postgres 15, the public role will no longer have the create privilege on the schema public by default.在即将发布的 Postgres 15 中,默认情况下,public 角色将不再具有对public模式的create权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM