如何删除除postgres中的少数几个数据库外的所有数据库 [英] how to drop all databases except few ones in postgres

查看:139
本文介绍了如何删除除postgres中的少数几个数据库外的所有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除除少数数据库以外的所有数据库。
可以说有20个数据库,我想从其中删除18个,但是保留2个,因为它是最新的数据库并且正在使用中。

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.

请提出建议

推荐答案

首先,在psql终端中执行以下查询。

First, execute the following query in the psql terminal.

select 'drop database "'||datname||'";'
from pg_database
where datistemplate=false;

这将为所有生成 drop数据库命令数据库。将结果复制到文本编辑器中,并排除(删除)要保留的内容,并将其另存为 dd.sql 文件。并像这样执行它:

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:

psql -d postgres -f dd.sql

这篇关于如何删除除postgres中的少数几个数据库外的所有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆