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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Connect Django to the Shops Database

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 settings.py.

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.

00:00 Welcome to video number five in this series on making a location-based web application with Django and GeoDjango. In this video, we’re going to connect our shops project in Django to our shops database in PostgreSQL.

00:18 In addition to having your shops database up and running, you should also have completed the work in video number four to set up your Django shops project, which serves as the root of your website.

00:29 Also, GeoDjango should be added to the INSTALLED_APPS area of settings.py.

00:37 Python cannot natively connect to a Postgres database, so we will have to install third-party drivers with pip. The package commonly used for this as psycopg2. To be fair, I don’t know if I’m pronouncing that correctly, but that’s what I’ve always called it.

00:53 There are different varieties of psycopg2, and you might at first think all you need to do is install the basic one. However, this one is not really complete, as it requires a C library at runtime called libpq.

01:05 This puts an unnecessary dependency on the host operating system. Luckily, psycopg2 is also available as a binary that includes this library and makes our lives easier.

01:16 For this reason, you will pip install psycopg2-binary. Two things to note, however: First, psycopg2-binary is not intended to be a dependency on published, maintained modules. Second, you will most likely need to upgrade pip in order to install it. Let’s do that now.

01:36 Open your terminal and in your geoshops/ directory, enter the command pip install -U pip.

01:48 With pip upgraded, this now clears the way for us to pip install psycopg2-binary.

01:57 Perfect! It’s now time to return to settings.py to complete the connection. In addition to specifying the driver, we will also need to know the following information before modifying our settings: the name of our database, a username and password Django can use to access the database, the host where the database is located, and the port through which to access it.

02:20 No problem. We have that information, right? Well, yes, but the only Postgres account we have so far is the superuser account that we set up when we installed PostgreSQL.

02:31 Let’s be a little more secure and create an account specific to our shops database. To create the new account, you’ll need to launch pgAdmin.

02:40 If it’s been a while since you’ve done so, let’s review how to do that. If pgAdmin is not already running, you may launch it from the PostgreSQL folder in your app menu.

02:50 If it is already running, you will see the elephant head logo in your task bar, from which you can launch a new admin window. In pgAdmin, select the shops database and then from the top menu choose Tools > Query Tool. From here, you will enter the SQL to create the account.

03:09 Let’s call the user shopadmin and give it the password 'shoppass'. We then enter a second command to give shopadmin rights on the shops database.

03:21 After that, we execute our query by clicking the lightning bolt.

03:29 Our new account doesn’t appear right away in the Login/Group Roles, but it will if we refresh the console.

03:40 Now our information is complete and we can update our settings file.

03:48 Open settings.py in your editor and scroll down to the DATABASES variable key. By default, Django is set up to access a SQLite database.

03:57 You will need to change the SQLite 'ENGINE' to the PostGIS backend. Overwrite the rest of the SQLite information with the specifics you’ve collected for your Postgres database connection.

04:18 You’ve accomplished quite a bit, but coming up, this project is really going to start to come alive. We will be creating a Django app. This might sound confusing—aren’t we doing that already?

04:29 Well, from Django’s perspective, an app as a component of a website that serves a particular purpose. You’ll understand more about what apps are and how to use them in the next video.

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. initd.org/psycopg/docs/install.html#binary-install-from-pypi

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: realpython.com/learning-paths/become-python-web-developer/

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 manage.py 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 manage.py startapp nearbyshop

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

Become a Member to join the conversation.