我已经使用 go sql 包成功连接到 Postgres 数据库:
...
db, err := sql.Open("postgres", connStr)
然后我使用返回的数据库执行(长时间运行的)查询:
rows, err := db.Query(...)
我得到了错误:
dial tcp xx.xxx.xxx.xx:5432: connect: connection timed out
我有几个问题:
- 为什么连接超时?
- 我能做些什么来防止它超时吗?
最佳答案
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/