Skip to content

Instantly share code, notes, and snippets.

@happysundar
Created October 23, 2014 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save happysundar/7dfefc971859dcf8d140 to your computer and use it in GitHub Desktop.
Save happysundar/7dfefc971859dcf8d140 to your computer and use it in GitHub Desktop.
posgresql function to cleanup the schemas within a database
DROP FUNCTION IF EXISTS cleanup( ) CASCADE;
CREATE OR REPLACE FUNCTION
cleanup()
RETURNS VOID
AS $$
DECLARE
row RECORD ;
BEGIN
FOR row IN WITH T1 AS (SELECT nspname :: TEXT AS schema_name
FROM pg_catalog.pg_namespace), T2 AS (SELECT schema_name
FROM T1
WHERE schema_name LIKE 'unbound_%') SELECT *
FROM T2
LOOP
RAISE LOG 'DROP SCHEMA % CASCADE', row.schema_name;
EXECUTE 'DROP SCHEMA '|| quote_ident(row.schema_name) || ' CASCADE';
END LOOP;
END
$$ LANGUAGE plpgsql;
SELECT cleanup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment