1

I have a flask application which I can run locally fine using flask_sqlalchemy with a SQLITE DB.

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'

I am now deploying this to Google App Engine. I have created a postgres db in CloudSQL.

How can I connect to the cloudsql database?

1 Answer 1

2

You don't specify if you are using the standard or the flex environment in App engine but in general to connect Cloud SQL Postgres to App Engine you can use the sqlalchemy library.

Code for reference extracted from the Public Documentation. The complete version can be found in the Cloud Platform GitHub

# The SQLAlchemy engine will help manage interactions, including automatically
# managing a pool of connections to your database
db = sqlalchemy.create_engine(
    # Equivalent URL:
    # postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
    sqlalchemy.engine.url.URL(
        drivername='postgres+pg8000',
        username=db_user,
        password=db_pass,
        database=db_name,
        query={
            'unix_sock': '/cloudsql/{}/.s.PGSQL.5432'.format(
                cloud_sql_connection_name)
        }
    ),
    # ... Specify additional properties here.
    # ...
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.