How do I drop all tables whose name start with, say, doors_? Can I do some sort of regex using the drop table command?
I prefer not writing a custom script but all solutions are welcomed. Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This script will generate the DDL commands to drop them all:
The cast
t.oid::regclassmakes the syntax work for mixed case identifiers, reserved words or special characters in table names, too. It also prevents SQL injection and prepends the schema name where necessary. More about object identifier types in the manual.About the schema search path.
You could automate the dropping, too, but it’s unwise not to check what you actually delete before you do.
You could append
CASCADEto every statement to DROP depending objects (views and referencing foreign keys). But, again, that’s unwise unless you know very well what you are doing. Foreign key constraints are no big loss, but this will also drop all dependent views entirely. WithoutCASCADEyou get error messages informing you which objects prevent you from dropping the table. And you can then deal with it.