无法从Golang连接到docker postgres容器

无法从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

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://*.com/a/54636760/8645590 worked for me.