[英]Can not connect from golang to docker postgres container
I spin up the docker container for postgres 我启动了docker容器用于postgres
docker run -i -t -v=":/var/lib/postgresql" -p 5432:5432 my_image/postgresql:9.3
And verify that it is reachable from host using 并使用验证主机是否可以访问
psql -h my_docker_ip -p 5432 -U pguser -W pgdb // passowrd: pguser
Now I want to connect to the container postgres
using go
in my host machine. 现在,我想使用主机中的
go
连接到容器postgres
。
import (
"database/sql"
_ "github.com/lib/pq"
"fmt"
)
func main() {
db, err := sql.Open("postgres", "user=pguser password='pguser' host=192.168.99.100 port=5432 sslmode=verify-full")
if err != nil {
fmt.Println(err)
}
rows, err := db.Query("SELECT * FROM test")
fmt.Println(rows)
}
While there were no error on initializing the db instance reference, the test query itself print out 尽管初始化数据库实例引用没有错误,但测试查询本身会打印出来
<nil>
This should not happen because I created table test
and multiple rows in table test
prior to running the go code. 这不应该发生,因为我创建的表
test
和多行表test
运行Go代码之前。
Can someone tell me what I am doing wrong? 有人可以告诉我我在做什么错吗?
Thanks 谢谢
I saw the same error when using mysql client in golang: 我在golang中使用mysql客户端时看到了相同的错误:
Failed to connect to database: x509: cannot validate certificate for 10.111.202.229 because it doesn't contain any IP SANs
The solution in https://stackoverflow.com/a/54636760/8645590 worked for me. https://stackoverflow.com/a/54636760/8645590中的解决方案为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.