本页介绍了如何将 PostgreSQL psycopg3 驱动程序连接到 Spanner 中的 PostgreSQL 方言数据库。psycopg3
是 PostgreSQL 的 Python 驱动程序。
确保 PGAdapter 与使用 PostgreSQL psycopg3 驱动程序进行连接的应用在同一台机器上运行。
如需了解详情,请参阅启动 PGAdapter。
在
psycopg3
连接属性中,将localhost
和5432
指定为数据库服务器主机和端口。- 可选:如果 PGAdapter 配置为监听默认 PostgreSQL 端口 (5432) 以外的端口,请指定其他端口号。
- 可选:如果 PGAdapter 在与本地计算机不同的主机上运行,请指定其他主机名。
connection = psycopg.connect(dbname="DATABASE_ID", host="localhost", port=5432) cursor = connection.cursor() cursor.execute('select \'Hello World\'') for row in cursor: print(row) cursor.close() connection.close()
后续步骤
- 详细了解 PGAdapter。
- 如需详细了解 PostgreSQL psycopg3 驱动程序连接选项,请参阅 PGAdapter GitHub 代码库中的 psycopg3 连接选项。