Python 如何在列表中获取现有postgres数据库

Python 如何在列表中获取现有postgres数据库,python,postgresql,Python,Postgresql,我正在尝试编写一个python脚本,以自动执行使用最新的生产转储创建数据库的任务。我正在使用psychopg2 但是在创建新数据库之后,我想删除以前使用的数据库。我的想法是,如果我可以在列表中获得数据库的名称并对它们进行排序,我就可以轻松地删除不需要的数据库 所以,我的问题是:如何在列表中获得DBs的名称 谢谢您可以使用 SELECT d.datname as "Name", FROM pg_catalog.pg_database d ORDER BY 1; 你可以按你喜欢的方式过滤或排序 p

我正在尝试编写一个python脚本,以自动执行使用最新的生产转储创建数据库的任务。我正在使用psychopg2

但是在创建新数据库之后,我想删除以前使用的数据库。我的想法是,如果我可以在列表中获得数据库的名称并对它们进行排序,我就可以轻松地删除不需要的数据库

所以,我的问题是:如何在列表中获得DBs的名称


谢谢

您可以使用

SELECT d.datname as "Name",
FROM pg_catalog.pg_database d
ORDER BY 1;
你可以按你喜欢的方式过滤或排序

psql-E-U postgres-c“\l”

上述命令的输出如下

********* QUERY **********
SELECT d.datname as "Name",
       pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
       pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
       d.datcollate as "Collate",
       d.datctype as "Ctype",
       pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;
**************************

                              List of databases
     Name     |  Owner   | Encoding | Collate | Ctype |   Access privileges   
--------------+----------+----------+---------+-------+-----------------------
 mickey       | postgres | UTF8     | C       | C     | 
 mickeylite   | postgres | UTF8     | C       | C     | 
 postgres     | postgres | UTF8     | C       | C     | 
 template0    | postgres | UTF8     | C       | C     | =c/postgres          +
              |          |          |         |       | postgres=CTc/postgres
 template1    | postgres | UTF8     | C       | C     | =c/postgres          +
              |          |          |         |       | postgres=CTc/postgres
(5 rows)

我一直在谷歌上搜索。但是还没有找到有用的帖子。当你更新数据库时,你会创建一个新的数据库吗?把它放进现有的数据库不是更好吗?@drcolosos:是的,对不起。我正在创建一个新的数据库。我无法更改现有数据库,因为我正在使用数据库的RoR应用程序正在使用它