Flask and psycopg2 : Pre-existing App Get Data

This article shows how to run a Python app with psycopg2 which uses PostgreSQL based Heroku Connect


Figure 1 : Heroku-Connect Data flow

Figure 1 show the how HerokuConnect Add-On interacts with Heroku Postgres and force.com behind the scenes Make sure you have Python installed. Also, install the Heroku Toolbelt

We will be using a basic Flask app available at flask_psycopg2_v1

This app has two rest endpoints

  • @app.route('/') which displays Hello world
  • @app.route('/contacts') which fetches list of contacts from Postgres table salesforce.contact

Clone the Source Code

$ git clone https://github.com/rajdeepd/flask-psycopg2-v1

Install Virtual Environment

Go to the root directory of the project cloned in the previous step and create a virtual environment env.

$ cd flask-psycopg2-v1
$ virtualenv venv
$ source venv/bin/activate

Install Dependencies

  1. Install the dependencies using pip install command

    $ pip install flask gunicorn psycopg2
  2. Run the app using the following command

    $ python app.py

    Your app should now be running on localhost:5000

You will see the default HelloWorld output as shown below.


If you try to access /contacts endpoint you will get a blank list.


Requirements File

Notice that the Requirements file already exists, this will be used by Heroku to setup the dynos

$ cat requirements.txt


There is already a Procfile which tells the Heroku what kind of Dyno is required and the source for the application.

web: gunicorn app:app --log-file -

Deploying to Heroku

$ heroku create
$ git push heroku master
$ heroku open

Add PostgreSQL Add-On

Add Postgress Add-On as shown below

$ heroku addons:create heroku-postgresql:hobby-dev

Add Heroku Connect Add-On

Configure Heroku Connect Add-On. Command below configures Herok-Connect Add-On to the application.

$ heroku addons:create herokuconnect

Open the addon in Heroku Dashboard

$  heroku addons:open herokuconnect

Configure Herok Connect Add-On

  1. Setup Connection
  1. Enter Schema Name : This is the schema name underwhich database will be created.
  1. Trigger OAuth
  1. Enter Salesforce.com developer account credentials
  1. Create Mappings
  1. Create Mappings Contacts : Choose the fields in Salesforce Schema which need to be mapped to Postgres Database in the application.
  1. Explore Contacts in the Dashboard

Open the App again in Heroku

$ heroku open

Show Contacts

Browse to URL http://{your-app-name}.herokuapp.com/contacts to see the list of contact names.


Show Contacts locally

Configure the DATABASE_URL in the local environment

$ heroku config

=== rocky-hamlet-29356 Config Vars
DATABASE_URL:      postgres://<user_name>:<password>@<ipaddress>.compute-1.amazonaws.com:5432/<database_name>


$ export DATABASE_URL=postgres://<user_name>:<password>@<ipaddress>.compute-1.amazonaws.com:5432/<database_name>

Open the following URL http://localhost:5000/contacts you should be able see the contacts.


In this tutorial we learnt how to configure a Pre-existing Python Flask Application to work with Heroku Connect. We used Psycopg2 driver for talking to the PostgreSQL database deployed on Heroku.