When trying to connect with the following settings I get a socket:operation not permitted
error on app engine, but not locally when using cloud-sql-proxy. The following code works locally but not with app engine:
connString = "user=USER_NAME password=PASSWORD host=/cloudsql/INSTANCE_NAME dbname=DB_NAME"
DBCon, err2 = sql.Open("postgres", connString)
Both the app engine and cloud sql are in the same project. Does anyone have any insight on what I am missing?
My App Engine projects use MySQL, but connections should theoretically be the same. Here is how I connect. Let me know if this doesn't work with Postgres
EDIT for 2nd gen instances
connString:= "@unix(/cloudsql/<your instance>)"
if appengine.IsDevAppServer() {
connString = "@tcp(<instance ip>:port)"
}
sqlUser := "<your user>"
sqlPass := "<your pass>"
connString = sqlUser + ":" + sqlPass + connString
// connect main db
db, err := sqlx.Connect("mysql", connString)
if err != nil {
return nil, err
}
Old string for 1st gen: connString := "@cloudsql(<your instance>)"
Unfortunately, Cloud SQL+postgres is not supported on AppEngine standard yet:
"Note: Connection from an App Engine standard environment application to a PostgreSQL instance is not supported"
https://cloud.google.com/sql/docs/postgres/connect-app-engine
NOTE: This is for mysql second version db
main.go
import (
_ "github.com/go-sql-driver/mysql",
"database/sql"
)
func InitDB() (err error, db *DB) {
db, err = sql.Open("mysql", os.Getenv("SQL_STRING"))
}
SQL_STRING: 'username:password@cloudsql(project:region:instance_id)/db'
For more ref https://cloud.google.com/appengine/docs/standard/go/cloud-sql/reference