作者:瀚高PG实验室 (Highgo PG Lab)- 徐云鹤
PG如何禁止在数据库执行drop table的操作?
创建触发器函数:
CREATE OR REPLACE FUNCTION DISABLE_DROP_TABLE()
RETURNS event_trigger
LANGUAGE plpgsql
AS $$
BEGIN
if tg_tag = 'DROP TABLE' THEN
RAISE EXCEPTION 'Command % is disabled.', tg_tag;
END if;
END;
$$;
创建事件触发器:
CREATE EVENT TRIGGER DISABLE_DROP_TABLE on ddl_command_start EXECUTE FUNCTION DISABLE_DROP_TABLE();
尝试执行drop table 命令报错:
postgres=# drop table aab;
ERROR: Command DROP TABLE is disabled.
CONTEXT: PL/pgSQL function disable_drop_table() line 4 at RAISE
其他操作也可通过类似方式进行限制。
更多的事件类型可查阅:
https://www.postgresql.org/docs/13/event-triggers.html