我如何在python中获取Postgresql中的数据库列表 [英] How can i get the list of databases in Postgresql in python

查看:50
本文介绍了我如何在python中获取Postgresql中的数据库列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 Postgresql 服务器中python列表中所有数据库的列表.基本上,那么我想创建那些索引另一个数据库但是我无法得到它.

I want to get the list of all databases in Postgresql server in python list. Basically then i want to create those insode another database But i am not able to get it.

这是我尝试过的

config_read = {
          'host':'psql-002',
          'database':'tesdb',
          'user':'pguser',
          'port':'5432',
          'password':'mypass'
          }


class ExportPSQL():

    def __init__(self):
        try:
            conn = psycopg2.connect(**config_read) 
            self.conn = conn
        except:
            print "Eror Connecting database"  

    def run(self):
    # Get a list of databases with :
        db_command=" psql -h localhost -U pguser tesdb -c '\l'"
        db_list = os.popen(db_command)

推荐答案

使用 pg_dumpall

Do it all with pg_dumpall and psql as superuser

pg_dumpall -h localhost -U postgres -f pg_dumpall.sql

如果您只想复制模式,请使用-仅schema 参数.

If you want to copy only the schema then use the --schema-only parameter.

要将其还原到另一个群集

To restore it to another cluster

psql -f pg_dumpall.sql postgres

这篇关于我如何在python中获取Postgresql中的数据库列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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