Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 13
    Thanks for posting this! I couldn't use the drop schema trick as the user was not owner of the schema, only of the tables. This one worked though :) Commented Nov 22, 2016 at 10:36
  • 1
    Very clean and specific... great solution, and should be the accepted too - you can even add to the where clause to limit tables you want to keep, as in those needed by extensions such as PostGIS... Commented Nov 18, 2017 at 15:46
  • 3
    I would suggest to change that line EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE'; with this: EXECUTE format('DROP TABLE IF EXISTS %I CASCADE', quote_ident(r.tablename)); Commented Oct 18, 2018 at 15:03
  • @tyger Why? It looks like an unnecessary complication to me. Is there an injection possibility (and does this really fix one if there is)? [I don't know if Postgres is foolish enough to allow table names to make that possible] If there is, you should really change your comment to an edit in the answer (explaining why in the edit comments). Commented May 30, 2019 at 16:05
  • EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE;'; this works better for foreign keys Commented Jul 18, 2020 at 21:44