Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
Created December 2, 2014 09:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save balazs-endresz/f9b27514046506a7db75 to your computer and use it in GitHub Desktop.
Save balazs-endresz/f9b27514046506a7db75 to your computer and use it in GitHub Desktop.
Drop all tables and sequences in postgres
# tables for owned by 'tableowner'
select 'drop table if exists "' || tablename || '" cascade;' from pg_tables where tableowner='tableowner';
# all sequences
select 'drop sequence if exists "' || relname || '" cascade;' from pg_class where relkind = 'S';
@elkevincorrea
Copy link

👎 🙂

@elkevincorrea
Copy link

💯

@EltonRst
Copy link

#All tables in current_schema(), replace by your schema name if you're not in current_schema()

DO $$ DECLARE
    r RECORD;
BEGIN
    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 $$;

#All sequences

DO $$ DECLARE
    r RECORD;
BEGIN
    FOR r IN (SELECT relname FROM pg_class where relkind = 'S') LOOP
        EXECUTE 'DROP SEQUENCE IF EXISTS ' || quote_ident(r.relname) || ' CASCADE';
    END LOOP;
END $$;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment