Each Answer to this Q is separated by one/two green lines.
I’ve looked over Google Cloud SQL’s documentation and various searches, but I can’t find out whether it is possible to use SQLAlchemy with Google Cloud SQL, and if so, what the connection URI should be.
I’m looking to use the Flask-SQLAlchemy extension and need the connection string like so:
I saw the Django example, but it appears the configuration uses a different style than the connection string. https://developers.google.com/cloud-sql/docs/django
Google Cloud SQL documentation:
Google Cloud SQL now supports direct access, so the
MySQLdb dialect can now be used. The recommended connection via the mysql dialect is using the URL format:
mysql+gaerdbms has been deprecated in SQLAlchemy since version 1.0
I’m leaving the original answer below in case others still find it helpful.
For those who visit this question later (and don’t want to read through all the comments), SQLAlchemy now supports Google Cloud SQL as of version 0.7.8 using the connection string / dialect (see: docs):
I have proposed an update to the
mysql+gaerdmbs:// dialect to support both of Google Cloud SQL APIs (
rdbms_googleapi) for connecting to Cloud SQL from a non-Google App Engine production instance (ex. your development workstation). The change will also modify the connection string slightly by including the project and instance as part of the string, and not require being passed separately via
This will also make it easier to use Cloud SQL with Flask-SQLAlchemy or other extension where you don’t explicitly make the
If you are having trouble connecting to Google Cloud SQL from your development workstation, you might want to take a look at my answer here – https://stackoverflow.com/a/14287158/191902.
If you find any bugs in SA+Cloud SQL, please let me know. I wrote the dialect code that was integrated into SQLAlchemy. There’s a bit of silly business about how Cloud SQL bubbles up exceptions, so there might be some loose ends there.
Please make sure to
Add the SQL instance to your
beta_settings: cloud_sql_instances: <PUT-SQL-INSTANCE-CONNECTION-NAME-HERE>
Enable the SQL Admin API as it seems to be necessary:
For Local Development
given that you started the Cloud SQL Proxy with:
it is doable, though I haven’t used Flask at all so I’m not sure about establishing the connection through that. I got it working through Pyramid and submitted a patch to SQLAlchemy (possibly to the wrong repo) here:
That has since been replaced and accepted into SQLAlchemy as
I don’t think it’s made it way to a release though.
There are some issues with Google SQL throwing different exceptions so we had issues with things like deploying a database automatically. You also need to disable connection pooling using NullPool as mentioned in the second patch.
We’ve since moved to using the datastore through NDB so I haven’t followed the progess of these fixes for a while..
PostgreSQL, pg8000 and flask_sqlalchemy
Adding information in case someone is on the lookout how to use
pg8000 as driver, the working connection string is