postgresql - 连接 : connection timed out

标签 postgresql go

我已经使用 go sql 包成功连接到 Postgres 数据库:

...
db, err := sql.Open("postgres", connStr)

然后我使用返回的数据库执行(长时间运行的)查询:

rows, err := db.Query(...)

我得到了错误:

dial tcp xx.xxx.xxx.xx:5432: connect: connection timed out

我有几个问题:

  1. 为什么连接超时?
  2. 我能做些什么来防止它超时吗?

最佳答案

sql.Open() may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping.

sql.Open() 函数只创建了一个对象,您的池目前是空的。简单来说,与数据库的连接尚未建立。

您需要调用 db.Ping() 以确保您的池具有有效连接。

关于postgresql - 连接 : connection timed out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56037887/

相关文章:

syntax - Go中字符串文字中的变量捕获?

performance - golang json/gob/xml 中的序列化性能

database - 执行批量更新和删除操作时避免 PostgreSQL 死锁

sql - 复合键每组行的序列号

sql - `SELECT a.*, array_agg(b)` `GROUP BY` 中的所有列都需要 `a`

amazon-web-services - DynamoDB 多个筛选条件,给出错误 - buildTree 错误 : unset parameter: ConditionBuilder

map - 运行时错误 : assignment to entry in nil map

golang 解析 yaml 文件结构受到挑战

ruby-on-rails - 用于删除反斜杠的 Postgres 命令在控制台中有效,但不适用于 ActiveRecord::Base.connection.execute

sql - PostgreSQL 根据数组值的组合选择行