将 pgx 连接到 PostgreSQL 方言数据库
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面介绍了如何将 PostgreSQL pgx 驱动程序连接到 PostgreSQL-dialect 数据库
。pgx
是 PostgreSQL 的 Golang 驱动程序。
确保 PGAdapter 与
使用 PostgreSQL pgx 驱动程序进行连接的应用。
如需了解详情,请参阅启动 PGAdapter。
指定 localhost
和 5432
作为数据库服务器主机和
pgx
连接字符串。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-07-19。
[{
"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":"其他"
}]