Skip to content

Instantly share code, notes, and snippets.

@ngoldenberg
Created July 11, 2019 16:14
Show Gist options
  • Save ngoldenberg/1deca236e609fd077883b9f12997ef89 to your computer and use it in GitHub Desktop.
Save ngoldenberg/1deca236e609fd077883b9f12997ef89 to your computer and use it in GitHub Desktop.
Delete/Drop multiple tables based on multiple prefixes - PostgreSQL
-- 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