Skip to content

Instantly share code, notes, and snippets.

@mlshvdv
Created November 13, 2018 21:51
Show Gist options
  • Save mlshvdv/cdcf94da3393ea518d192644ae4af7fb to your computer and use it in GitHub Desktop.
Save mlshvdv/cdcf94da3393ea518d192644ae4af7fb to your computer and use it in GitHub Desktop.
Drop all PostgreSQL tables by one query
DO $$
DECLARE
r record;
BEGIN
FOR r IN SELECT quote_ident(tablename) AS tablename, quote_ident(schemaname) AS schemaname FROM pg_tables WHERE schemaname = 'public'
LOOP
RAISE INFO 'Dropping table %.%', r.schemaname, r.tablename;
EXECUTE format('DROP TABLE IF EXISTS %I.%I CASCADE', r.schemaname, r.tablename);
END LOOP;
END$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment