Created
July 11, 2019 16:14
-
-
Save ngoldenberg/1deca236e609fd077883b9f12997ef89 to your computer and use it in GitHub Desktop.
Delete/Drop multiple tables based on multiple prefixes - PostgreSQL
This file contains 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
-- Inspect the generated statements before you actually execute: comment RAISE and uncomment the EXECUTE | |
DO | |
$do$ | |
DECLARE | |
_tbl text; | |
BEGIN | |
FOR _tbl IN | |
SELECT c.oid::regclass::text -- escape identifier and schema-qualify! | |
FROM pg_catalog.pg_class c | |
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | |
WHERE n.nspname NOT LIKE 'pg_%' -- exclude system schemas | |
AND c.relname SIMILAR TO '%(GENERAL|indicators|ZONING)%' || '%' -- your tables names prefixes | |
AND c.relkind = 'r' -- only tables | |
LOOP | |
RAISE NOTICE '%', | |
-- EXECUTE | |
'DROP TABLE ' || _tbl; | |
END LOOP; | |
END | |
$do$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment