postgresql 如何删除postgres中除少数数据库之外的所有数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24548297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to drop all databases except few ones in postgres
提问by ManishD
I want to drop all of the databases except few ones. Lets say there are 20 databases and I want to delete 18 out of them but keep 2 as it is the latest ones and are in use.
我想删除除少数数据库之外的所有数据库。假设有 20 个数据库,我想删除其中的 18 个,但保留 2 个,因为它是最新的并且正在使用中。
Please suggest.
请建议。
回答by ntalbs
First, execute the following query in the psql terminal.
首先,在 psql 终端中执行以下查询。
select 'drop database "'||datname||'";'
from pg_database
where datistemplate=false;
This will generate drop database
command for all the databases. Copy the result in a text editor and exclude(delete) what you want to keep and save it as dd.sql
file. And execute it like this:
这将为drop database
所有数据库生成命令。在文本编辑器中复制结果并排除(删除)您要保留的内容并将其另存为dd.sql
文件。并像这样执行它:
psql -d postgres -f dd.sql
回答by kraymer
As accepted answer kinda demonstrates it, dropping multiple databases was particularly tedious for me, so I wrote an helper script to alleviate this operation : https://github.com/Kraymer/ezdropdb
正如接受的答案所表明的那样,删除多个数据库对我来说特别繁琐,因此我编写了一个帮助脚本来减轻此操作:https: //github.com/Kraymer/ezdropdb
In short, you enter a pattern that the databases you want to suppress must match then all db names results are listed and there is a final prompt where you can enter which ones of those to drop (cf screenshot on project page) .
简而言之,您输入一个模式,您要禁止的数据库必须匹配,然后列出所有 db 名称结果,并且有一个最终提示,您可以在其中输入要删除的那些(参见项目页面上的屏幕截图)。