Connecting Google App Engine to Cloud SQL

78 views
Skip to first unread message

Jason Lee

unread,
Jun 22, 2017, 3:25:56 PM6/22/17
to Google App Engine
I am trying to follow the tutorial at:

And I am having the hardest time making my Google App Engine (GoLang) connect to the Cloud SQL Postgres database. I am able to connect NOT following the tutorial by manually opening the port from the firewall rules. But I cannot do it this way because it's connected via IP and if I do it this way, I cannot deploy to GAE.

Could someone please review the following GoLang code to see what is wrong.

```
This following line is used to connect from localhost GoLang app to Cloud SQL -- NOT FOLLOWING THE TUTORIAL
db, err := sql.Open("postgres", host=IP_ADDRESS port=PORT user=USERNAME password=PASSWORD dbname=DBNAME sslmode=disable)

Here, I am connecting to the Cloud SQL postgres database by IP address. I opened a port manually by firewall rule to make it accessible from my local IP.
This works great but I cannot "deploy" my GoLang app to Google App Engine so this is not possible.

---

This does NOT work. It follows the tutorial. I tried 4 different variations.

connectionName = "CONNECTION_NAME"
db, err := sql.Open("postgres", fmt.Sprintf("%s:%s@cloudsql(%s)", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("%s:%s@cloudsql(%s)/DB_NAME", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@cloudsql(%s)", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@cloudsql(%s)/DB_NAME", user, password, connectionName))

All four of these variations do not work. And I do not know why. I could really use some help understanding why. The only difference between this and the tutorial is that I'm using "postgres", not "mysql".
```

Thank you so kindly in advance.

Jason Lee

unread,
Jun 22, 2017, 3:44:54 PM6/22/17
to Google App Engine

Josh Moore

unread,
Jun 22, 2017, 4:03:22 PM6/22/17
to google-a...@googlegroups.com
If you take a look at the CloudSQL Postgres documentation, you'll see that App Engine standard is not yet supported.  Postgres on CloudSQL is currently a beta product and isn't available for all environments at this time.  You can, however, use a Postgres db on App Engine Flexible environment.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+unsubscribe@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/2cc425fa-b87c-409b-b423-a7b8a3bc1065%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Josh Moore | Cloud Solutions Engineer | josh...@google.com | 

Jason Lee

unread,
Jun 22, 2017, 4:11:46 PM6/22/17
to Google App Engine
I am using App Engine flexible environment.

Does this mean there's no way for me to connect to Google Cloud SQL from App Engine once deployed?



On Thursday, 22 June 2017 13:03:22 UTC-7, joshuamo wrote:
If you take a look at the CloudSQL Postgres documentation, you'll see that App Engine standard is not yet supported.  Postgres on CloudSQL is currently a beta product and isn't available for all environments at this time.  You can, however, use a Postgres db on App Engine Flexible environment.
On Thu, Jun 22, 2017 at 12:44 PM, Jason Lee <jason...@gmail.com> wrote:
Tutorial link I am following is: https://cloud.google.com/appengine/docs/standard/go/cloud-sql/#setting-up



On Thursday, 22 June 2017 12:25:56 UTC-7, Jason Lee wrote:
I am trying to follow the tutorial at:

And I am having the hardest time making my Google App Engine (GoLang) connect to the Cloud SQL Postgres database. I am able to connect NOT following the tutorial by manually opening the port from the firewall rules. But I cannot do it this way because it's connected via IP and if I do it this way, I cannot deploy to GAE.

Could someone please review the following GoLang code to see what is wrong.

```
This following line is used to connect from localhost GoLang app to Cloud SQL -- NOT FOLLOWING THE TUTORIAL
db, err := sql.Open("postgres", host=IP_ADDRESS port=PORT user=USERNAME password=PASSWORD dbname=DBNAME sslmode=disable)

Here, I am connecting to the Cloud SQL postgres database by IP address. I opened a port manually by firewall rule to make it accessible from my local IP.
This works great but I cannot "deploy" my GoLang app to Google App Engine so this is not possible.

---

This does NOT work. It follows the tutorial. I tried 4 different variations.

connectionName = "CONNECTION_NAME"
db, err := sql.Open("postgres", fmt.Sprintf("%s:%s@cloudsql(%s)", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("%s:%s@cloudsql(%s)/DB_NAME", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@cloudsql(%s)", user, password, connectionName))
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@cloudsql(%s)/DB_NAME", user, password, connectionName))

All four of these variations do not work. And I do not know why. I could really use some help understanding why. The only difference between this and the tutorial is that I'm using "postgres", not "mysql".
```

Thank you so kindly in advance.

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.

Josh Moore

unread,
Jun 22, 2017, 5:01:40 PM6/22/17
to google-a...@googlegroups.com
The tutorial you linked was for the App Engine standard environment, which is why it isn't working in the Flexible environment.  I would look over the App Engine Flexible golang docs for Using CloudSQL, and the App Engine Flexible python docs for Using CloudSQL Postgres.  I'll put in a request to add a sample for go, so this is an easier task to just get started with.

I haven't tested this myself, but it looks like your connection should look like:
db, err := sql.Open("postgres", fmt.Sprintf("%s:%s@%s?host=/cloudsql/%s", user, password, database, connectionName))


To unsubscribe from this group and stop receiving emails from it, send an email to google-appengine+unsubscribe@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jason Lee

unread,
Jun 23, 2017, 1:38:05 AM6/23/17
to Google App Engine
Thank you,

That code snippet you gave did not work, but I will wait for a GoLang example.

Reply all
Reply to author
Forward
0 new messages