[英]How to call DROP USER from pl/pgsql?
How to call DROP USER
from Pl/PgSql (see example)?如何从 Pl/PgSql 调用
DROP USER
(见示例)?
CREATE PROCEDURE myfunc()
LANGUAGE PLPGSQL AS
$$
DECLARE
super_users TEXT[];
ldap_users TEXT[];
u TEXT;
BEGIN
super_users := ARRAY(SELECT usename::TEXT FROM pg_catalog.pg_user WHERE usesuper);
ldap_users := ARRAY(SELECT uid::TEXT FROM ldap_users);
FOREACH u IN ARRAY ldap_users LOOP
IF (u <> 'postgres' AND u <> ALL(super_users)) THEN
DROP USER IF EXISTS u;
END IF;
END LOOP;
END;
$$;
It leads to error that "role u does not exist"...它会导致“角色 u 不存在”的错误...
IMHO PL/PGSQL does not treat u
as a variable, but as a name.恕我直言,PL/PGSQL 不将
u
视为变量,而是将其视为名称。 And DROP USER...
is not SQL but some extension.而
DROP USER...
不是 SQL 而是一些扩展。 How to do it?怎么做? Maybe some system function?
也许某些系统 function? Or special syntax to substitute
u
?或者特殊的语法来代替
u
?
EDIT: My solution (just found):编辑:我的解决方案(刚刚找到):
DECLARE
stm TEXT;
...
BEGIN
...
stm := 'DROP USER IF EXISTS "' || u '"';
EXECUTE stm;
...
It seems to work.它似乎工作。 Maybe there is other solutions?
也许还有其他解决方案? More canonical?
更规范?
You need dynamic SQL for this:为此,您需要动态 SQL :
execute format('DROP USER IF EXISTS %I', u);
Try this:尝试这个:
DROP OWNED BY user;
DROP ROLE user;
DROP USER user;
It works for me.这个对我有用。
Pd: After checking SOF, you can try: Pd:检查SOF后,可以尝试:
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM user;
REVOKE ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public FROM user;
REVOKE ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public FROM user;
DROP USER user;
Cheers mate.队友的欢呼声。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.