简体   繁体   English

是否可以使用golang灵活地从app引擎连接到google cloud sql postgres实例?

[英]Is it possible to connect to a google cloud sql postgres instance from app engine flexible using golang?

I was able to find This document which explains how to do it from python : https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres 我能够找到这个文档,它解释了如何从python中做到这一点: https//cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres

I could not find anything equivalent for golang. 我找不到任何与golang相同的东西。 Is this even possible? 这甚至可能吗?

It is possible. 有可能的。

Disclaimer : I have not tried to actually do that, so there may be typos / small errors in what follows. 免责声明 :我没有尝试过这样做,因此下面可能会出现拼写错误/小错误。 Also I'd like to assure you that CloudSQL documentation team is aware and working on adding examples for other languages including Go. 另外,我想向您保证,CloudSQL文档团队了解并正在为其他语言(包括Go)添加示例。

Under the guts AppEngine Flex runs CloudSQL proxy for you and your code connects to CloudSQL via usual driver using unix socket. 根据内容,AppEngine Flex为您运行CloudSQL代理 ,您的代码使用unix socket通过常用驱动程序连接到CloudSQL。

So while there is no example for Go, it should be fairly easy. 所以虽然没有Go的例子,但它应该相当容易。 Assuming you use standard Go's sql library and reading https://godoc.org/github.com/lib/pq looks like you need something like this in your code: 假设您使用标准Go的sql库并阅读https://godoc.org/github.com/lib/pq ,您的代码中需要这样的内容:

db, err := sql.Open("postgres", "user=username password=password host=/cloudsql/INSTANCE_CONNECTION_NAME dbname=databasename")

Or, if you use different format or library, just make sure to specify /cloudsql/INSTANCE_CONNECTION_NAME for host parameter. 或者,如果您使用不同的格式或库,只需确保为主机参数指定/cloudsql/INSTANCE_CONNECTION_NAME

Also, make sure to follow all non-Python specific steps of https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres . 此外,请务必遵循https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres的所有非Python特定步骤。

PS: here's example for Django: Postgres settings for Django on Google App Engine Flexi PS:这是Django的例子: Google App Engine Flexi上 Django的Postgres设置

The accepted answer above is correct, but I just wanted to note here that Google has now added official documentation that explains the same thing in details here 上面接受的答案是正确的,但我只是想在此注意到谷歌现在已经添加了官方文档,详细解释了相同的内容

https://cloud.google.com/appengine/docs/flexible/go/using-cloud-sql-postgres https://cloud.google.com/appengine/docs/flexible/go/using-cloud-sql-postgres

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Engine Golang 连接到 Cloud SQL Postgres unix dial no such file or directory - Google App Engine Golang connect to Cloud SQL Postgres unix dial no such file or directory 无法从 App Engine 连接到 Google Cloud SQL 中的 postgres,该 App Engine 在 Docker 中使用自定义运行时运行 .NET 核心应用程序 - Unable to connect to postgres in Google Cloud SQL from App Engine running a .NET Core app in Docker with Custom runtime 使用 psycopg2 连接到 App Engine (Google Cloud) 上的 Postgres - Connect to Postgres on App Engine (Google Cloud) using psycopg2 如何将Google App Engine(Flex)Go​​应用连接到Google Cloud Postgres实例 - How to connect Google App Engine (Flex) Go app to Google Cloud Postgres instance 从梁管道连接谷歌云 sql postgres 实例 - connect google cloud sql postgres instance from beam pipeline 我可以从 Cloud Run 连接到 Compute Engine 中托管的 Postgres 实例吗? (不是 Cloud SQL) - Can I connect from Cloud Run to a Postgres instance hosted in Compute Engine? (not Cloud SQL) 从应用引擎实例连接到Google云SQL - Connecting to a google cloud SQL from an app engine instance 从 App Engine Flexible 连接到 Cloud SQL 时出现问题 - Problem while connecting to Cloud SQL from App Engine Flexible Google App Engine Rails Postgres 无法连接到 Cloud SQL 数据库:没有这样的文件或目录 - Google App Engine Rails Postgres won't connect to Cloud SQL database: No such file or directory 无法将 nodejs 应用引擎连接到 postgres 云 sql - cannot connect nodejs app engine to postgres cloud sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM