postgresql - Google App Engine Golang 连接到 Cloud SQL Postgres unix 拨号没有这样的文件或目录

标签 postgresql google-app-engine go google-cloud-sql

大家好,我在将我的 golang 应用程序连接到云 sql postgres 实例时遇到了问题。我刚刚完成了他们在 Google Cloud Platform 中的教程和示例代码,但它似乎不起作用。我只有两个文件 app.yaml 和 main.go 只是为了测试连接:

应用.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

主.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
}

我尝试使用 go run 。和 goapp 发球。我也尝试过添加 appengine 导入,但似乎没有任何效果,而且它一直给我错误:

Could not query db: dial unix /cloudsql/bxustl2019proj:asia-east1:sqlstlbxu/stlbxudbs/.s.PGSQL.5432: connect: no such file or directory

Cloud SQL Admin 也已启用。我只是注释掉了 env 以便于运行,无论硬编码查询路径还是从 yaml 导入,错误似乎都是一样的。

我希望有人能帮我解决这个问题。谢谢。

最佳答案

看起来您正在连接 App Engine(但您没有指定标准或灵活环境)。

如果您使用的是标准版,则 unix 套接字在 /cloudsql/<INSTANCE_CONNECTION_NAME> 处自动可用。 .

如果您使用的是 Flexible,则需要在 app.yaml 中指定要连接的实例,这需要在 /cloudsql/<INSTANCE_CONNECTION_NAME> 处创建 unix 套接字。 .

这些 un​​ix 套接字仅在运行时本身中提供。如果您在本地运行并想要连接,则需要使用 Cloud SQL proxy/cloudsql/<INSTANCE_CONNECTION_NAME> 创建 unix 套接字(或更新您的连接字符串以使用公共(public) IP 并以不同的方式进行身份验证)。

关于postgresql - Google App Engine Golang 连接到 Cloud SQL Postgres unix 拨号没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55203147/

相关文章:

java - 使用 c3p0 : createClob() is not yet implemented hibernate

performance - 是否有关于 Google App Engine 或其他云平台性能的报告或论文

python - GAE GCS write 是否像 NDB 函数一样具有异步版本

go - Go中调用kernel32的ReadProcessMemory

sql - 在单个查询中分组依据和排序依据

postgresql - Peewee:怎么做 ORDER BY ... NULLS (FIRST|LAST)?

android - 我应该在 Android 客户端中验证 OAuth2 token ,还是 App Engine 本身会根据传递给后端 API 的凭据对用户进行身份验证?

debugging - GoLand远程调试显示 "could not find <file>"

postgresql - Docker-compose Go 应用程序和 Postgres 之间的通信问题

postgresql - PostgreSQL 中的嵌套连接与合并连接与哈希连接