Can SQLAlchemy be used with Google Cloud SQL?

Sean Lynch picture Sean Lynch · May 26, 2012 · Viewed 17.3k times · Source

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: mysql://username:password@server/db

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: https://developers.google.com/cloud-sql/docs/developers_guide_python

Answer

Sean Lynch picture Sean Lynch · Jun 5, 2012

Update

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+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>

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):

mysql+gaerdbms:///<dbname>

E.g.:

create_engine('mysql+gaerdbms:///mydb', connect_args={"instance":"myinstance"})

I have proposed an update to the mysql+gaerdmbs:// dialect to support both of Google Cloud SQL APIs (rdbms_apiproxy and 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 connect_args.

E.g.

mysql+gaerdbms:///<dbname>?instance=<project:instance>

This will also make it easier to use Cloud SQL with Flask-SQLAlchemy or other extension where you don't explicitly make the create_engine() call.

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.