postgresql - 如何将 Google App Engine (Flex) Go 应用程序连接到 Google Cloud Postgres 实例

标签 postgresql google-app-engine go

我正在使用 Go 构建应用程序并使用 Google App Engine 进行部署。

我已经在 Google Cloud 上设置了一个 PostgreSQL 实例,启用了 API 并使用本地计算机上的 SQL 代理成功连接到它,包括本地 PSequel 客户端和我的应用程序。

但是,当我执行 gcloud app deploy 时,出现此错误:

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

我已尝试将 Postgres 实例从版本 11 Beta 降级到 9.6。也尝试过使用此驱动程序 https://github.com/broady/gae-postgres ,这看起来很有希望,但我无法在 flex 环境中工作。

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")

}

最佳答案

我在我的测试环境中重现了这个问题,发现您遇到的错误是由于 beta_settings 指定了端口。我在最后用 =tcp:5432 尝试了它,它给了我同样的错误,然后我在没有它的情况下尝试了它并且部署没有任何问题。

关于postgresql - 如何将 Google App Engine (Flex) Go 应用程序连接到 Google Cloud Postgres 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57396825/

相关文章:

go - 如何以编程方式确定 pod 是否处于 crashloopbackoff 状态

postgresql - 不触发 INSERT 触发器的 INSERT 语句

Django 管理数据错误 : Value too long for type character varying(150)

go - 从子包访问全局变量

google-app-engine - 何时在 GAE 的数据存储中使用实体组

google-app-engine - 在 "unexpected error"中使用 google cloud 添加计费方式

带有重复标签的xml解析

Django ContentTypes 将 uuid 字段转换为整数字段

javascript - 当我尝试在 Heroku 上使用 POSTGRESQL 更新数据库时,出现 22001 错误

python - 谷歌基本 API 的 401 和 403 错误