0
votes

I'm building an app in Go and am using Google App Engine for deployment.

I have set up a PostgreSQL instance on Google Cloud, have enabled the API and have successfully connected to it using the SQL proxy from my local machine, both from a local PSequel client and from my app.

When I do gcloud app deploy, however, I get this error:

ERROR: (gcloud.app.deploy) Error Response: [9] 
Application startup error:
panic: dial unix /cloudsql/sapling:europe-west1:sapling/.s.PGSQL.5432: connect: no such file or directory

I have tried downgrading the Postgres instance from version 11 Beta to 9.6. Have also tried using this driver https://github.com/broady/gae-postgres, which seemed promising but I couldn't get working for the flex environment.

app.yaml

runtime: go
#Here we select the AppEngine Flex environment which performs a lot of the backend preparation for us including the ability to scale give the demand
env: flex
env_variables:
  POSTGRES_CONNECTION: "user=postgres password=thisisntmypwd dbname=postgres host=/cloudsql/sapling:europe-west1:sapling"
manual_scaling:
  instances: 1
#Select resource size
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10
beta_settings:
  cloud_sql_instances: sapling:europe-west1:sapling=tcp:5432

config/db.go

package config

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
    "os"
)

var Db *sql.DB

//Set up connection to the database
func init() {
    var err error
    datastoreName := os.Getenv("POSTGRES_CONNECTION")
    Db, err = sql.Open(
        "postgres",
        datastoreName,
        )
    if err != nil {
        panic(err)
    }


    err = Db.Ping()
    if err != nil {
        panic(err)
    }

    fmt.Println("Connected to database")

}
1
Thanks for the helpful downvote, which I presume was for the mangled code formatting. I have fixed that now. Or was there something else you didn't like? - Matt
Can you check if Cloud SQLAdmin API is enabled for the project? If you still have the error afterwards, can you try it again with the flag --verbosity=debug? this can give you more details of the error - alan
Thanks @alan. I checked. Cloud SQL API was enabled, but not Cloud SQLAdmin API. I enabled it but didn't seem to make any difference. Still the same error. Ran with the verbosity flag but no new info around the db connection, still the same error as above - Matt

1 Answers

0
votes

I replicated this issue in my testing environment and found that the error you are getting is due to the beta_settings having the port specified. I tried it with the =tcp:5432 at the end, and it gave me the same error, then I tried it without it and it got deployed without any issues.