Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Connect Django to the Shops Database

Give Feedback

In this video we’re going to connect our shops project in Django to our shops database in PostgreSQL.

In addition to having your shops database up and running, you should also have completed the work in video 4 to set up your Django shops project which serves as the root of your website. Also GeoDjango should be added to the INSTALLED_APPS area of

Python can not natively connect to a Postgres database so we will have to install third party drivers with pip.

The package commonly used for this is psycopg2.

There are different varieties of psycopg2 and you might at first think all you need is the basic one, however this one is not really complete as it requires a C library at runtime called libpq. This puts an unnecessary dependency on the host operating system.

Luckily pyscopg2 is also available as a binary that includes this library and makes our lives easier. For this reason, you will pip install pyscopg2-binary. Two things to note, however. First, pyscopg2-binary is not intended to be a dependency on published, maintained modules. Second, you will most likely need to upgrade pip to install it. Let’s do that now.

Comments & Discussion

Abigail on April 30, 2019

When you say that psycopg2-binary is not intended to be used as a dependency in published, maintained modules, would that apply to a Django app in production? If so, what would be recommended in this environment?

Jackie Wilson RP Team on April 30, 2019

Hi Abigail! The documentation explains the library conflicts and recommends building from source. I have a Django app running in production with this dependency, and haven’t had any problems, but it’s a hobby site and I’m lazy.

Bishnu on May 20, 2019

hi Jackie, Nice explanation.I am a beginner into python world and i love random topics so i choose this one and i have completed all setup in my machine.can you suggest some articles or video course as a prerequisite. Thanks, Bishnu

Jackie Wilson RP Team on May 20, 2019

Hi Bishnu, thanks for watching the course. Our Django course gets great reviews and I would recommend that as a pre-req. It’s available as the first lesson here:

arifams on Nov. 15, 2019

Jackie thanks a lot for the tutorial. It’s great!

Just sharing for fellow learner. I got error after doing : python startapp nearbyshop

And this is the error in terminal (using mac catalina): raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)

I tried to replace the /usr/lib/libpq.5.dylib library as a stackoverflow advice but it doesn’t work. So I changed the psycopg2 binary. What I did as workaround are:

  1. Install pipenv to localized the installation pip install pipenv

  2. Install old psycopg2 binary pipenv install "psycopg2-binary<=2.8" (the latest release at the moment is 2.8.4)

  3. Then after that create the nearbyshop python startapp nearbyshop

It works for me and I hope it helps to other here :)

Become a Member to join the conversation.