\dn --shows your schema name
SELECT session_user, current_user; --shows your user
--create a regular table with some data in it:
drop table if exists mytable;
CREATE TABLE mytable( index integer, foo character varying(10));
insert into mytable values (3, 'yatta');
insert into mytable values (9, 'phobos');
select * from mytable;
┌───────┬────────┐
│ index │ foo │
├───────┼────────┤
│ 3 │ yatta │
│ 9 │ phobos │
└───────┴────────┘
--Make a rule to stop deletes on a table:
CREATE or replace RULE mytable_del_protect AS ON DELETE TO mytable
DO INSTEAD NOTHING;
--Try to do a delete on the table
delete from mytable where index = 3; --delete succeeds and affects 0 rows
select * from mytable; --row you deleted is still there
--Clear the rule:
drop RULE mytable_del_protect on mytable;
--once again try to delete
delete from mytable where index = 3; --delete succeeds, affects 1 row
┌───────┬────────┐
│ index │ foo │
├───────┼────────┤
│ 9 │ phobos │
└───────┴────────┘
列出 postgresql 规则:
您的规则信息被编码在pg_rewritepg_classand中pg_namespace:
select n.nspname as rule_schema, c.relname as rule_table,
case r.ev_type
when '1' then 'SELECT'
when '2' then 'UPDATE'
when '3' then 'INSERT'
when '4' then 'DELETE'
else 'UNKNOWN'
end as rule_event
from pg_rewrite r join pg_class c on r.ev_class = c.oid
left join pg_namespace n on n.oid = c.relnamespace
where c.relname = 'mytable'
┌─────────────┬────────────┬────────────┐
│ rule_schema │ rule_table │ rule_event │
├─────────────┼────────────┼────────────┤
│ public │ mytable │ DELETE │
└─────────────┴────────────┴────────────┘
在 PostgreSQL 中,只有数据库的所有者才能删除数据库。(超级用户可以删除数据库,但这是一个不同的问题。)因此,更改所有者是防止 user1 删除任何数据库的最直接方法。
使用ALTER DATABASE修复它。
要将特定权限还给 user1,请使用GRANT。您可能正在寻找类似的东西
但请先阅读 GRANT 的文档。您可能需要 WITH GRANT OPTION、序列权限、低于 ALL PRIVILEGES 的权限等等。
Postgresql 规则阻止对象上发生某种事件:
列出 postgresql 规则:
您的规则信息被编码在
pg_rewrite
pg_class
and中pg_namespace
:您可以调用一个过程,而不是规则对阻止的删除不做任何事情。您可以针对任何类型的数据库对象上的任何类型的事件创建一个规则来执行任何操作。