【问题标题】:Python - Postgres - psycopg2 - ThreadedConnectionPool - Connect to primary in HA clusterPython - Postgres - psycopg2 - ThreadedConnectionPool - 连接到 HA 集群中的主节点
【发布时间】:2023-02-08 23:23:20
【问题描述】:

Python/psycopg2/ThreadedConnectionPool 是否接受一个参数,告诉它只连接到 Postgres HA 集群中的主服务器?等效的 jdbc 连接字符串看起来像,注意目标服务器类型=主要:

jdbc:postgresql://serverName1:5432,serverName2:5432,serverName3:5432/dataBaseName?loadBalanceHosts=false&targetServerType=primary

如何在下面的连接设置中将 targetServerType 传递给 ThreadedConnectionPool:

self._pg_pool = psycopg2.pool.ThreadedConnectionPool(
    minconn   = 1,
    maxconn   = 2,
    host      = 'serverName1, serverName2, serverName3',
    database  = 'dataBaseName1',
    user      = 'userName',
    password  = 'passWord',
    port      = "5432",
    application_name = 'MyTestApp'
)

【问题讨论】:

    标签: python postgresql psycopg2


    【解决方案1】:

    结果你只需要将以下内容添加到 ThreadedConnectionPool 构造函数中......

    target_session_attrs='primary'
    

    所以连接看起来像......

    self._pg_pool = psycopg2.pool.ThreadedConnectionPool(
        minconn   = 1,
        maxconn   = 2,
        host      = 'serverName1, serverName2, serverName3',
        database  = 'dataBaseName1',
        user      = 'userName',
        password  = 'passWord',
        port      = "5432",
        application_name = 'MyTestApp',
        target_session_attrs='primary'
    )
    

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2020-12-14
      • 2018-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多