我需要删除我的数据库中的所有模式,除了public
,information_schema
和那些LIKE 'pg_%'
.
这是我发现的:(这个变种似乎不起作用)
CREATE OR REPLACE FUNCTION drop_all ()
RETURNS VOID AS
$$
DECLARE rec RECORD;
BEGIN
-- Get all the schemas
FOR rec IN
SELECT DISTINCT schemaname
FROM pg_catalog.pg_tables
-- You can exclude the schema which you don't want to drop by adding another condition here
WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'public'
AND schemaname != 'information_schema'
LOOP
EXECUTE 'DROP SCHEMA ' || rec.schemaname || ' CASCADE';
END LOOP;
RETURN;
END;
$$ LANGUAGE plpgsql;
SELECT drop_all();
另一种变体:(这个可能有效,但仍然使我的应用程序崩溃)
SELECT string_agg(format('DROP SCHEMA %I CASCADE;', nspname), E'\n')
FROM pg_namespace WHERE nspname != 'public'
AND nspname NOT LIKE 'pg_%'
AND nspname != 'information_schema';
所以,我想确保查询是正确的,或者希望找到查询的另一个变体。
有人可以帮我找到另一个查询吗?