How to drop multiple tables in PostgreSQL using a wildcard

前端 未结 7 1978
闹比i
闹比i 2020-12-13 05:39

When working with partitions, there is often a need to delete all partitions at once.

However

DROP TABLE tablename*

Does not work.

7条回答
  •  鱼传尺愫
    2020-12-13 06:15

    Disclosure: this answer is meant for Linux users.

    I would add some more specific instructions to what @prongs said:

    • \dt can support wildcards: so you can run \dt myPrefix* for example, to select only the tables you want to drop;
    • after CTRL-SHIFT-DRAG to select then CTRL-SHIFT-C to copy the text;
    • in vim, go to INSERT MODE and paste the tables with CTRL-SHIFT-V;
    • press ESC, then run :%s/[ ]*\n/, /g to translate it to comma-separated list, then you can paste it (excluding the last comma) in DROP TABLE % CASCADE.

提交回复
热议问题