[英]connect: connection timed out
I have successfully connected to a Postgres database using the go sql package: 我已经使用go sql包成功连接到Postgres数据库:
...
db, err := sql.Open("postgres", connStr)
I then use the returned database to execute a (long running) query: 然后,我使用返回的数据库执行(长期运行)查询:
rows, err := db.Query(...)
And am getting the error: 并得到错误:
dial tcp xx.xxx.xxx.xx:5432: connect: connection timed out
I have a couple of questions regarding this: 我对此有两个问题:
sql.Open() may just validate its arguments without creating a connection to the database. sql.Open()可以仅验证其参数, 而无需创建与数据库的连接 。 To verify that the data source name is valid, call Ping . 要验证数据源名称是否有效, 请调用Ping 。
The sql.Open()
function has only created an object, your pool is currently empty. sql.Open()
函数仅创建了一个对象,您的池当前为空。 In simple words connection with the database hasn't been established yet. 简而言之,尚未建立与数据库的连接。
You need to call db.Ping()
to make sure your pool has a working connection. 您需要调用db.Ping()
来确保您的池具有有效的连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.