将 psycopg2 连接到 PostgreSQL 方言数据库

本页面介绍了如何将 PostgreSQL psycopg2 驱动程序连接到 PostgreSQL 方言数据库 。psycopg2 是适用于 PostgreSQL 的 Python 驱动程序。

  1. 确保 PGAdapter 与 使用 PostgreSQL psycopg2 驱动程序进行连接的应用。

    如需了解详情,请参阅启动 PGAdapter

  2. 指定 localhost5432 作为数据库服务器主机和 psycopg2 连接属性。

    • (可选)如果 PGAdapter 为 配置为在非默认 PostgreSQL 端口上进行监听 端口 (5432)。
    • (可选)如果 PGAdapter 正在运行,请指定其他主机名 与本地机器位于不同主机上
    connection = psycopg2.connect(database="database_name",
                                 host="localhost",
                                 port=5432)
    
    cursor = connection.cursor()
    cursor.execute('select \'Hello World\'')
    for row in cursor:
     print(row)
    
    cursor.close()
    connection.close()
    

后续步骤