Difficulty: Beginner
Estimated Time: 10 minutes

This tutorial demonstrates how to use PostgreSQL together with the Python web application framework Django. It is an adaptation of Richard Yen's tutorial found at: https://www.enterprisedb.com/postgres-tutorials/how-use-postgresql-django


You have data that you need to display on the web. A Postgres database is available to store the data. You have a basic familiarity with Python, basic Linux commands, and with SQL.


  1. Katacoda's UI and some data to work with
  2. Creating a virtual Python environment
  3. Installing Django
  4. Installing the psycopg Postgres adapter
  5. Connecting Django to Postgres
  6. Configuring our Django project
  7. Creating a Django app
  8. Defining data models to represent our data
  9. Creating and running a schema migration
  10. Creating a Django views to present our data
  11. Setting up URL routing for our Django app
  12. Run the app!

Prefer to review the code this locally? You can find all the material used in this tutoral on GitHub: (TODO: link)

(Please give the Katacoda environment a minute to set up - this will go away once we've built a proper base environment)

Developing a basic Django + Postgresql Web Application

Step 1 of 12

Connecting to and querying Postgres

Katacoda's UI

We'll work through this tutorial using the file editor to the top-right, and the terminal to the bottom-right. The system we're working with is based on Ubuntu Linux, you're logged in with root privileges, and most standard commands are available.

From time to time, I'll include commands that must be run in the terminal - you can type these in yourself, or click the listing to run it. Let's start by setting up a directory to work in under /var/projects:

mkdir /var/projects/myproject
cd /var/projects/myproject

When you see a link to a file in this tutorial, clicking it will open it in the editor. You may also browse through most files below /var/projects using the tree shown to the left of the editor.

Let's edit cars.csv (click that link!)

Add some data to that file by clicking "Copy to Editor" on the listing below:


In some cases, you'll need to write or copy text into a file, so let's do the next one a bit differently.

Open: drivers.csv

The listing below can be clicked to copy the text to your clipboard.

1,John Doe,Z1234567
2,Jane Doe,Z9876543

Paste that into drivers.csv, please - we'll use this data later on!

Note: file are saved as soon as you make changes to them.