Skip to content

Instantly share code, notes, and snippets.

@yolabingo
Created June 10, 2021 16:11
Show Gist options
  • Save yolabingo/eecd2e455db38149a7d3c99c3966f466 to your computer and use it in GitHub Desktop.
Save yolabingo/eecd2e455db38149a7d3c99c3966f466 to your computer and use it in GitHub Desktop.
delete all tables from a posgres database
DO $$ DECLARE
r RECORD;
BEGIN
-- if the schema you operate on is not "current", you will want to
-- replace current_schema() in query with 'schematodeletetablesfrom'
-- *and* update the generate 'DROP...' accordingly.
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment