0

I have a postgres database schema with dozens of tables i need to drop. I essentially need to drop all the tables in my schema (abcd) with the word "group" in the name. How do i do that without manually going in and writing drop script for each one? Wondering if there is some easy way to do that in postgres sql? or maybe using python?

drop table abcd.group%;

1
  • select * from pg_class where relnamespace = 'abcd'::regnamespace and relname ilike 'group%' will get you tables starting with group. If you want group anywhere in name then '%group%'. You could then loop over those in Python or a Postgres function. Commented Nov 3, 2020 at 22:37

1 Answer 1

1

Well this is probably not the coolest way to do this, but you can run the query below, copy and paste the output and run that.

SELECT 'DROP TABLE ' || schemaname || '.' || relname || ';' "statement"
FROM pg_catalog.pg_statio_user_tables
WHERE schemaname = 'abcd' AND relname ILIKE 'group%';
Sign up to request clarification or add additional context in comments.

2 Comments

what is "statement"? is that a specific command in postgres?
I just can leave a column without a name. Otherwise it has no meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.