doulin6448 2017-01-22 03:54
浏览 126

无法从Golang连接到docker postgres容器

I spin up the docker container for 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.

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.

Can someone tell me what I am doing wrong?

Thanks

  • 写回答

1条回答 默认 最新

  • dongzhao2725 2019-02-11 18:25
    关注

    I saw the same error when using mysql client in golang:

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗