将 pgx 连接到 PostgreSQL 方言数据库
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面介绍如何将 PostgreSQL pgx 驱动程序连接到 Spanner 中的 PostgreSQL 方言数据库。pgx
是适用于 PostgreSQL 的 Golang 驱动程序。
确保 PGAdapter 与使用 PostgreSQL pgx 驱动程序进行连接的应用在同一机器上运行。
如需了解详情,请参阅启动 PGAdapter。
在 pgx
连接字符串中指定 localhost
和 5432
作为数据库服务器主机和端口。pgx
要求在连接字符串中提供用户名和密码。PGAdapter 会忽略这些项。
- (可选)如果 PGAdapter 配置为在默认 PostgreSQL 端口 (5432) 以外的端口上进行监听,则请指定其他端口号。
- 默认情况下,PGAdapter 会停用 SSL。默认情况下,
pgx
会首先尝试在启用 SSL 的情况下进行连接。在连接请求中停用 SSL 可加快连接过程,因为它需要的往返减少一次。
connString := "postgres://uid:pwd@localhost:5432/my-database?sslmode=disable"
ctx := context.Background()
conn, err := pgx.Connect(ctx, connString)
if err != nil {
return err
}
defer conn.Close(ctx)
var greeting string
err = conn.QueryRow(ctx, "select 'Hello world!' as hello").Scan(&greeting)
if err != nil {
return err
}
fmt.Printf("Greeting from Cloud Spanner PostgreSQL: %v\n", greeting)
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-03-15。
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻译问题"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"易于理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"解决了我的问题"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]