英文:
Is it possible to connect to a google cloud sql postgres instance from app engine flexible using golang?
问题
我找到了这篇文档,它解释了如何在Python中完成这个操作:https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres
但是我没有找到类似的Go语言文档。这个操作是否可行呢?
英文:
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
I could not find anything equivalent for golang. Is this even possible?
答案1
得分: 5
可以的。
免责声明:我没有尝试实际操作,所以下面的内容可能会有拼写错误或小错误。此外,我想向您保证,CloudSQL文档团队已经意识到这一点,并正在努力添加其他语言(包括Go)的示例。
在AppEngine Flex中,底层运行着CloudSQL代理,您的代码通过常规驱动程序使用Unix套接字连接到CloudSQL。
因此,虽然没有Go的示例,但应该相当容易。假设您使用标准的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")
或者,如果您使用不同的格式或库,请确保将主机参数指定为/cloudsql/INSTANCE_CONNECTION_NAME
。
此外,请确保遵循https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres中的所有非Python特定步骤。
附注:这是Django的示例:https://stackoverflow.com/questions/42950418/postgres-settings-for-django-on-google-app-engine-flexi?rq=1
英文:
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.
Under the guts AppEngine Flex runs CloudSQL proxy for you and your code connects to CloudSQL via usual driver using unix socket.
So while there is no example for Go, it should be fairly easy. 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:
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.
Also, make sure to follow all non-Python specific steps of https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql-postgres.
PS: here's example for Django: https://stackoverflow.com/questions/42950418/postgres-settings-for-django-on-google-app-engine-flexi?rq=1
答案2
得分: 0
上面接受的答案是正确的,但我只想在这里指出,Google现在已经添加了官方文档,详细解释了相同的内容,链接在这里:
https://cloud.google.com/appengine/docs/flexible/go/using-cloud-sql-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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论