Created
July 8, 2014 05:48
-
-
Save happysundar/ac12dc5c49552ff2f0e4 to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/3327312/drop-all-tables-in-postgresql PL/PgSQL Function to drop all tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS remove_all(); | |
CREATE FUNCTION remove_all() RETURNS void AS $$ | |
DECLARE | |
rec RECORD; | |
cmd text; | |
BEGIN | |
cmd := ''; | |
FOR rec IN SELECT | |
'DROP SEQUENCE ' || quote_ident(n.nspname) || '.' | |
|| quote_ident(c.relname) || ' CASCADE;' AS name | |
FROM | |
pg_catalog.pg_class AS c | |
LEFT JOIN | |
pg_catalog.pg_namespace AS n | |
ON | |
n.oid = c.relnamespace | |
WHERE | |
relkind = 'S' AND | |
n.nspname NOT IN ('pg_catalog', 'pg_toast') AND | |
pg_catalog.pg_table_is_visible(c.oid) | |
LOOP | |
cmd := cmd || rec.name; | |
END LOOP; | |
FOR rec IN SELECT | |
'DROP TABLE ' || quote_ident(n.nspname) || '.' | |
|| quote_ident(c.relname) || ' CASCADE;' AS name | |
FROM | |
pg_catalog.pg_class AS c | |
LEFT JOIN | |
pg_catalog.pg_namespace AS n | |
ON | |
n.oid = c.relnamespace WHERE relkind = 'r' AND | |
n.nspname NOT IN ('pg_catalog', 'pg_toast') AND | |
pg_catalog.pg_table_is_visible(c.oid) | |
LOOP | |
cmd := cmd || rec.name; | |
END LOOP; | |
FOR rec IN SELECT | |
'DROP FUNCTION ' || quote_ident(ns.nspname) || '.' | |
|| quote_ident(proname) || '(' || oidvectortypes(proargtypes) | |
|| ');' AS name | |
FROM | |
pg_proc | |
INNER JOIN | |
pg_namespace ns | |
ON | |
(pg_proc.pronamespace = ns.oid) | |
WHERE | |
ns.nspname = | |
'public' | |
ORDER BY | |
proname | |
LOOP | |
cmd := cmd || rec.name; | |
END LOOP; | |
EXECUTE cmd; | |
RETURN; | |
END; | |
$$ LANGUAGE plpgsql; | |
SELECT remove_all(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment