[英]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.