How do I drop all tables in psql (PostgreSQL interactive terminal) that starts with a common word?

前端 未结 2 466
一个人的身影
一个人的身影 2021-01-14 21:23

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

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-14 21:44

    I normally use one query to generate the DDL commands for me based on some of the metadata tables and then run those commands manually. For example:

    SELECT 'DROP TABLE ' || tablename || ';' FROM pg_tables
    WHERE tablename LIKE 'prefix%' AND schemaname = 'public';
    

    This will return a bunch of DROP TABLE xxx; queries, which I simply copy&paste to the console. While you could add some code to execute them automatically, I prefer to run them on my own.

提交回复
热议问题