<p>I want to connect from Google Cloud Function to Kubernetes (GKE) container. Specifically, the container has postgres database and I want to read records in a table.</p>

<p>In Golang:</p>

<pre><code>func ConnectPostgres(w http.ResponseWriter, r *http.Request) {

    db, err := sql.Open("postgres", "postgresql://postgres@10.32.0.142:5432/myDatabase")

    if err != nil {
        http.Error(w, "Error opening conn:" + err.Error(), http.StatusInternalServerError)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        http.Error(w, "Error ping conn:" + err.Error(), http.StatusInternalServerError)
    }

    rows, err := db.Query("SELECT * FROM myTable")

    fmt.Println(rows)

    w.Write([]byte(rows))
}
</code></pre>

<p>10.32.0.142 is the Internal IP of the pod having the container.</p>

<p>But when the cloud function tries to Ping to postgres container, the request gets timed out.</p>

<p>How can I solve this?</p>

<p>You need to connect Cloud Function to VPC first, detailed here: <a href="https://cloud.google.com/functions/docs/connecting-vpc" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/connecting-vpc</a> </p>
