Heroku Connect makes it easy to build Heroku apps that share data with your Salesforce organization. This post shows you how to run a Python Flask app with Psycopg2 on Heroku, which uses Heroku Connect to sync data between PostgreSQL and Salesforce. Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Psycopg2 is a DB API 2.0 compliant PostgreSQL driver.

Figure 1: Heroku-Connect Data flow

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

Install the Virtual Environment

Create a folder flask-psycopg2-sample and install a virtual environment in it.

Install Dependencies

$ pip install flask gunicorn

Creating a Simple Flask App

  1. First Create a base Flask app with simple REST endpoint/ in a file app.py in the folder created above.

  2. Run the app using the following command

    Your app should now be running on localhost:5000

Initialize git

Initialize the git repository as shown by commands below.

Create a Requirements File

Create a Procfile

Create a file named Procfile in the root of the app and add the following content. Procfile specifies that the app uses a web dyno with gunicorn as HTTP server.

Deploying to Heroku

Add PostgreSQL Add-On

Add Postgress Add-On as shown below

Add the Heroku Connect Add-On

Configure the Heroku Connect Add-On. The command below configures the Heroku-Connect Add-On to the application.

Configure Heroku Connect Add-On

  1. Setup Connection by pressing the “Setup Connection” button as shown below

  1. Enter Schema Name: This is the schema name under which database will be created. Default is set to “salesforce”

  1. Trigger OAuth by clicking the Authorize button shown below

  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 by pressing Explorer button :

Add Code for contacts endpoint

First Add following lines which configure the Connection object conn to PostgreSQL Database.

Add code for the Getting the Contacts

Complete code listing:

Add Jinja Template

The code shown in the previous section uses template.html file which is a Jinja template. Add this file under folder templates

Update python packages

Add Requirements file

Create a Procfile

Create a Procfile which will be used to identify the web dyno and type of runtime

Update Changes in Heroku

Open the App again in Heroku

Show Contacts

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

Summary

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

About the Author

Rajdeep Dua is Director Developer Relations at Salesforce. He is passionate about helping developers learn about Cloud Computing and Salesforce. He has over 16 years of experience in Software Product Development and Developer Relations.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS