我想知道为什么允许新创建的用户连接到数据库后创建表。我有一个数据库,project2_core
:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+--------------+-----------+-------------+-------------+-------------------------------
postgres | postgres | SQL_ASCII | C | C |
project2_core | atm_project2 | UTF8 | de_DE.UTF-8 | de_DE.UTF-8 | project2=CTc/project2
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
到现在为止还挺好。现在,我创建一个用户:
postgres=# CREATE ROLE dietrich ENCRYPTED PASSWORD 'md5XXX' LOGIN NOCREATEROLE NOCREATEDB NOSUPERUSER
好的。当我尝试连接数据库时,不允许用户这样做:
$ psql -h localhost -p 5432 -U dietrich -W project2_core
Password for user dietrich:
psql: FATAL: permission denied for database "project2_core"
DETAIL: User does not have CONNECT privilege.
这是我所期望的。现在奇怪的东西开始了。我授予用户CONNECT
:
postgres=# GRANT CONNECT ON DATABASE project2_core TO dietrich;
GRANT
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------+--------------+-----------+-------------+-------------+-------------------------------
postgres | postgres | SQL_ASCII | C | C |
project2_core | atm_project2 | UTF8 | de_DE.UTF-8 | de_DE.UTF-8 | project2=CTc/project2+
| | | | | dietrich=c/project2
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
无需任何进一步的授权,用户就可以创建表:
$ psql -h localhost -p 5432 -U dietrich -W project2_core
Password for user dietrich:
psql (9.2.3)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
project2_core=> create table adsf ();
CREATE TABLE
project2_core=> \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+----------
public | adsf | table | dietrich
(1 row)
我曾希望在我GRANT USAGE
对模式然后GRANT SELECT
对表进行显式操作之前,不允许用户执行任何操作。
我的错误在哪里?我究竟做错了什么?我如何实现自己想要的(新用户在明确授予她适当的权限之前,不允许执行任何操作。
我迷路了,非常感谢您的帮助:)
编辑按照@ daniel-verite的建议,我现在在创建数据库后立即撤销所有操作。用户DietDirich不再可以创建表。好。但是:现在,数据库的所有者project2也不允许创建表。甚至在发出GRANT ALL PRIVILEGES ON DATABASE project2_core TO project2
and之后GRANT ALL PRIVILEGES ON SCHEMA public TO project2
,我仍然收到错误ERROR:未选择要在其中创建的架构,而当我专门尝试这样做时CREATE TABLE public.WHATEVER ();
,我得到了ERROR:架构public的权限被拒绝。我究竟做错了什么?
REVOKE ALL ON DATABASE project2_core FROM PUBLIC;
。为什么这没有效果?