如何从 pl/pgsql 调用 DROP USER?
How to call DROP USER from pl/pgsql?
如何从 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;
$$;
导致“角色你不存在”的错误...
恕我直言 PL/PGSQL 不将 u
视为变量,而是作为名称。 DROP USER...
不是 SQL 而是一些扩展。怎么做?也许一些系统功能?或者用特殊语法代替 u
?
编辑:
我的解决方案(刚找到):
DECLARE
stm TEXT;
...
BEGIN
...
stm := 'DROP USER IF EXISTS "' || u '"';
EXECUTE stm;
...
似乎有效。也许还有其他解决方案?更规范?
为此你需要 dynamic SQL:
execute format('DROP USER IF EXISTS %I', u);
试试这个:
DROP OWNED BY user;
DROP ROLE user;
DROP USER user;
对我有用
P.d:查看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;
干杯伙计。
如何从 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;
$$;
导致“角色你不存在”的错误...
恕我直言 PL/PGSQL 不将 u
视为变量,而是作为名称。 DROP USER...
不是 SQL 而是一些扩展。怎么做?也许一些系统功能?或者用特殊语法代替 u
?
编辑: 我的解决方案(刚找到):
DECLARE
stm TEXT;
...
BEGIN
...
stm := 'DROP USER IF EXISTS "' || u '"';
EXECUTE stm;
...
似乎有效。也许还有其他解决方案?更规范?
为此你需要 dynamic SQL:
execute format('DROP USER IF EXISTS %I', u);
试试这个:
DROP OWNED BY user;
DROP ROLE user;
DROP USER user;
对我有用
P.d:查看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;
干杯伙计。