开发者

Python如何管理多个PostgreSQL数据库的连接

开发者 https://www.devze.com 2025-03-27 13:21 出处:网络 作者: jzy3711
目录操作步骤1.导入相关库2. 配置PostgreSQL连接信息3. 定义连接函数4.解释关键点5.使用示例完整python总结主要实现了通过读取配置文件中的PostgreSQL服务信息,连接到相应的PostgreSQL数据库。提编程客栈供了两个连
目录
  • 操作步骤
    • 1.导入相关库
    • 2. 配置PostgreSQL连接信息
    • 3. 定义连接函数
    • 4.解释关键点
    • 5.使用示例
  • 完整python
    • 总结

      主要实现了通过读取配置文件中的PostgreSQL服务信息,连接到相应的PostgreSQL数据库。提编程客栈供了两个连接函数,postgresql_connectpostgresql_connect_encode,分别是普通的和带编码参数的连接函数。

      操作步骤

      1.导入相关库

      from pyhive import hive
      import cx_oracle
      import pymysql
      import pymssql
      from optparse import OptionParser
      import logging
      import sys
      import traceback
      import re
      import warnings
      with warnings.catch_warnings(record=True):
          import psycopg2
      reload(sys)
      

      2. 配置PostgreSQL连接信息

      这里定义了一个包含多个PostgreSQL服务配置信息的列表pgconfigs。每个配置项由数据库名称、用户、密码、主机和端口组成。

      pgconfigs = [
          {"asset_factory": {"database": "asset_factory", "user": "asset", pMuJWhxk"password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"asset_register": {"database": "asset_register", "user": "asset", "password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"creditdb": {"database": "creditdb", "user": "credit", "password": "hHJ98#pE40Y", "host": "10.251.101.175", "port": "18923"}},
          {"asset_portal": {"database": "asset_portal", "user": "asset", "password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"tyjh": {"database": "tyjh", "user": "tyjh", "password": "k5y2dwoKcFm&^OsW", "host": "10.251.90.35", "port": "18922"}},
          {"cwrl": {"database": "cwrl", "user": "cwjh_yaxin", "password": "eKYy4R3&MbaaY3Zy", "host": "10.251.90.34", "port": "18921"}},
          {"dc_new_pg": {"database": "dc", "user": "dc", "password": "Ojjkcy@jVxKIeo5C", "host": "10.251.90.36", "port": "18923"}},
          {"credit_data": {"database": "credit_data", "user": "credit_data", "password": "@G*1f*3$3dzTH6%o", "host": "10.251.90.134", "port": "18923"}},
          {"new_jf_pg_cd": {"database": "accthuiju", "user": "datacenter", "password": "KridU593&%rj90", "host": "133.37.116.192", "port": "18921"}},
          {"new_accthuiju": {"database": "accthuiju", "user": "itf_ods", "password": "2dH~fZ^8", "host": "10.251.64.226", "port": "18921"}},
          {"crm3huijupg": {"database": "crm3huijupg", "user": "datacenter", "password": "lp*36^YD", "host": "10.251.65.93", "port": "18921"}},
          {"crm3hispg": {"database": "crm3hispg", "user": "datacenter", "password": "~4nD_jWG", "host": "10.251.64.196", "port": "18921"}},
          {"hana": {"database": "hana", "user": "hana", "password": "gvptHKXVNKEGw7pk", "host": "10.251.90.94", "port": "18924"}},
          {"dataos_71_pg_dev": {"database": "dacp", "user": "dacp", "password": "jxFgCKv9GJw2ohS3", "host": "10.251.110.104", "port": "18921"}}
      ]
      

      3. 定义连接函数

      普通连接函数

      def postgresql_connect(servicename):
          global job_task_content_ori
          global job_resource_conn_info
          global job_hdfs_dir_info
          try:
              for subconfig in pgconfigs:
                  if servicename in subconfig:
                      pgconfig = subconfig[servicename]
                      conn = psycopg2.connect(database=pgconfig["database"], user=pgconfig["user"],
                                              password=pgconfig["password"], host=pgconfig["host"], port=pgconfig["port"])
                      return conn
              print("servicename %s not found in config dictionary" % servicename)
              sys.exit(6)
          except:
              traceback.print_exc()
              print("connect pg error " + pgconfig["database"])
              sys.exit(6)
      

      上述代码中的postgresql_connect函数:

      • 遍历pgconfigs列表,查找与参数servicename匹配的配置。
      • 使用匹配的配置建立PostgreSQL连接。
      • 如果未找到匹配的服务名,打印错误信息并退出程序。

      带编码参数的连接函数

      def postgresql_connect_encode(servicename, encoding='UTF-8'):
          global job_task_content_ori
          global job_resource_conn_info
          global job_hdfs_dir_info
          try:
              for subconfig in pgconfigs:
                  if servicename in subconfig:
                      pgconfig = subconfig[servicename]
                      conn = psycopg2.connect(database=pgconfig["database"], user=pgconfig["user"],
                                              password=pgconfig["password"], host=pgconfig["host"], port=pgconfig["port"],
                                              client_encoding=encoding)
                      return conn
              print("servicename %s not found in config dictionary" % servicename)
              sys.exit(6)
          except:
              traceback.print_exc()
              print("connect pg error " + pgconfig["database"])
              sys.exit(6)
      

      postgresql_connect_编程encode函数与前一个函数类似,唯一的区别是它接受一个可选的编码参数encoding,并将其传递给psycopg2.connect以设定客户端编码。

      4.解释关键点

      配置管理pgconfigs列表包含多个数据库配置,便于根据服务名快速查找匹配的配置项。

      错误处理:在连接失败时,打印堆栈跟踪以帮助调试并退出程序。

      编码管理:对于需要特定编码的连接,提供了额外的函数来设置编码。

      5.使用示例

      假设你要连接到名为 dataos_71_pg_dev 的PostgreSQL服务,可以像下面这样使用这些函数:

      # 普通连接
      conn = postgresql_connect("dataos_71_pg_dev")
      
      # 带编码参数的连接
      conn_with_encoding = postgresql_connect_encode("dataos_71_pg_dev", encoding='UTF-8')
      

      完整python

      #!/data/apps/python2715/bin/python
      # -*- coding:utf-8 -*-
      
      from pyhive import hive
      import cx_Oracle
      import pymysql
      import pymssql
      from optparse import OptionParser
      import logging
      import sys
      import traceback
      import re
      import warnings
      with warnings.catch_warnings(record=True):
          import psycopg2
      reload(sys)
      
      pgconfigs = [
          {"asset_factory": {"database": "asset_factory", "user": "asset", "password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"asset_register": {"database": "asset_register", "user": "asset", "password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"creditdb": {"database": "creditdb", "user": "credit", "password": "hHJ98#pE40Y", "host": "10.251.101.175", "port": "18923"}},
          {"asset_portal": {"database": "asset_portal", "user": "asset", "password": "Ro20ot16", "host": "10.251.80.202", "port": "65432"}},
          {"tyjh": {"database": "tyjh", "user": "tyjh", "password": "k5y2dwoKcFm&^OsW", "host": "10.251.90.35", "port": "18922"}},
          {"cwrl": {"database": "cwrl", "user": "cwjh_yaxin", "password": "eKYy4R3&MbaaY3Zy", "host": "10.251.90.34", "port": "18921"}},
          {"dc_new_pg": {"database": "dc", "user": "dc", "password": "Ojjkcy@jVxKIeo5C", "host": "10.251.90.36", "port": "18923"}},
          {"credit_data": {"database": "credit_data", "user": "credit_data", "password": "@G*1f*3$3DzTH6%o", "host": "10.251.90.134", "port": "18923"}},
          {"new_jf_pg_cd": {"database": "accthuiju", "user": "datacenter", http://www.devze.com"password": "KridU593&%rj90", "host": "133.37.116.192", "port": "18921"}},
          {"new_accthuiju": {"database": "accthuiju", "user": "itf_ods", "password": "2dH~fZ^8", "host": "10.251.64.226", "port": "18921"}},
          {"crm3huijupg": {"database": "crm3huijupg", "user": "datacenter", "password": "lp*36^YD", "host": "10.251.65.93", "port": "18921"}},
          {"crm3hispg": {"database": "crm3hispg", "user": "datacenter", "password": "~4nD_jWG", "host": "10.251.64.196", "port": "18921"}},
          {"hana": {"database": "hana", "user": "hana", "password": "gvptHKXVNKEGw7pk", "host": "10.251.90.94", "port": "18924"}},
          {"dataos_71_pg_dev": {"database": "dacp", "user": "dacp", "password": "jxFgCKv9GJw2ohS3", "host": "10.251.110.104", "port": "18921"}}
      ]
      
      
      def postgresql_connect(servicename):
          global job_task_content_ori
          global job_resource_conn_info
          global job_hdfs_dir_info
          try:
              for subconfig in pgconfigs:
                 if subconfig.has_key(servicename):
                      pgconfig = subconfig[servicename]
                      conn = psycopg2.connect(database=pgconfig["database"], user=pgconfig["user"],
                                              password=pgconfig["password"], host=pgconfig["host"], port=pgconfig["port"])
                      return conn
              print("servinamename %s not found in config dictionary" % servicename)
              sys.exit(6)
          except:
              traceback.print_exc()
              print("connect pg error " + pgconfig["database"])
              sys.exit(6)
      
      
      def postgresql_connect_encode(servicename, encoding='UTF-8'):
          global job_task_content_ori
          global job_resource_conn_info
          global job_hdfs_dir_info
          try:
              for subconfig in pgconfigs:
                  if subconfig.has_key(servicename):
                      pgconfig = subconfig[servicename]
                      conn = psycopg2.connect(database=pgconfig["database"], user=pgconfig["user"],
                                      javascript        password=pgconfig["password"], host=pgconfig["host"], port=pgconfig["port"],
                                              client_encoding=encoding)
                      return conn
              print("servinamename %s not found in config dictionary" % servicename)
              sys.exit(6)
          except:
              traceback.print_exc()
              print("connect pg error " + pgconfig["database"])
              sys.exit(6)

      总结

      这段代码展示了如何通过配置文件管理多个PostgreSQL数据库的连接,提供了普通连接和带编码参数的连接函数,确保了代码的灵活性和可重用性。通过处理异常和提供详细的错误信息,也提高了代码的可靠性和可维护性。

      以上就是Python如何管理多个PostgreSQL数据库的连接的详细内容,更多关于Python PostgreSQL数据库连接的资料请关注编程客栈(www.devze.com)其它相关文章!

      0

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      关注公众号