Good day everyone, I am having trouble in connecting my golang application to a cloud sql postgres instance. I have just followed through their tutorial in Google Cloud Platform and sample code but it seems to be not working. I have just two files the app.yaml and main.go just to test the connection:
app.yaml
runtime: go api_version: go1 env: standard
env_variables: CLOUDSQL_CONNECTION_NAME: bxustl2019proj:asia-east1:sqlstlbxu CLOUDSQL_USER: ustldbbxu CLOUDSQL_PASSWORD: bxuuserstldb CLOUDSQL_DB: stlbxudbs
beta_settings:
cloud_sql_instances: bxustl2019proj:asia-east1:sqlstlbxu
handlers:
- url: /(.*\.(gif|png|jpg))$ static_files: static/\1 upload: static/.*\.(gif|png|jpg)
- url: /.* script: _go_app
main.go
package dptest
import (
_"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"os"
_ "github.com/lib/pq"
)
var db *sql.DB
func init() {
db = DB()
http.HandleFunc("/", indexHandler)
}
// DB gets a connection to the database.
// This can panic for malformed database connection strings, invalid credentials, or non-existance database instance.
func DB() *sql.DB {
/*
var (
connectionName = mustGetenv("CLOUDSQL_CONNECTION_NAME")
user = mustGetenv("CLOUDSQL_USER")
dbname = mustGetenv("CLOUDSQL_DB")
password = os.Getenv("CLOUDSQL_PASSWORD") // NOTE: password may be empty
socket = os.Getenv("CLOUDSQL_SOCKET_PREFIX")
)
//cloudsql is used on App Engine.
if socket == "" {
socket = "/cloudsql"
}
*/
// PostgreSQL Connection, uncomment to use.
// connection string format: user=USER password=PASSWORD host=/cloudsql/PROJECT_ID:REGION_ID:INSTANCE_ID/[ dbname=DB_NAME]
//dbURI := fmt.Sprintf("user=%s password=%s host=/cloudsql/%s database=%s", user, password, connectionName, dbname)
dbURI := fmt.Sprintf("user=ustldbbxu password=bxuuserstldb host=/cloudsql/bxustl2019proj:asia-east1:sqlstlbxu/stlbxudbs")
conn, err := sql.Open("postgres", dbURI)
log.Printf("CONNECTION: %v", conn)
if err != nil {
panic(fmt.Sprintf("DB: %v", err))
}
rows, err := conn.Query("SELECT * FROM GAMES")
log.Printf("ROW: %v", rows)
if err != nil {
log.Printf("Could not query db: %v", err)
}
defer rows.Close()
return conn
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "text/plain")
log.Printf("CONNECTION: %v", db)
rows, err := db.Query("SELECT * FROM GAMES")
log.Printf("ROW: %v", rows)
if err != nil {
log.Printf("Could not query db: %v", err)
http.Error(w, "Internal Error", 500)
return
}
defer rows.Close()
}
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Panicf("%s environment variable not set.", k)
}
return v
}
I tried using go run . and goapp serve. I have tried adding appengine imports also but none seems to be working and it keeps on giving me the error:
Could not query db: dial unix /cloudsql/bxustl2019proj:asia-east1:sqlstlbxu/stlbxudbs/.s.PGSQL.5432: connect: no such file or directory
Cloud SQL Admin is also enabled. I just commented out the env for easier run the error seems to be the same regardless of hardcoding the query path or importing from yaml.
I am hoping someone could help me on this.Thank you.
It looks like you are connecting with App Engine (but you don't specify Standard or Flexible environments).
If you are on Standard, the unix socket is automatically available at /cloudsql/<INSTANCE_CONNECTION_NAME>
.
If you are on Flexible, you need to specify the instance you want to connect to in the app.yaml, which will need create the unix socket at /cloudsql/<INSTANCE_CONNECTION_NAME>
.
These unix sockets are only provided in the runtimes themselves. If you are running locally and want to connect, you will need to use the Cloud SQL proxy to create the unix socket at /cloudsql/<INSTANCE_CONNECTION_NAME>
(or update your connection string to use Public IP and authenticate a different way).