TLT: Setting up Postgres with Django on Windows

So, it's been a while. I said it'd be every 2 days, and I lagged behind. Got a few things done over the past few days. I've decided to go with Postgres for Alright: Remember that these are supposed to be clear posts, very short, to the point, meant for quick use and my own review.  "TLT" stands for "The Little Things."

Why Postgres

  • Heard lots of undertones of disgust and "blahs" whenever SQLite3 was mentioned. Such reactions were usually followed by high praise for Postgres. The same conversational phenomenon occurred for Mongo. This is all with respect to Django use, by the way.
  • Supposedly an excessive amount of wiggle room in terms of customization. That includes function customization, extensions, and custom data types.
  • Nice error messages, unlike those of MySQL, apparently. Complaints about MySQL in this respect are common, if we're to compare the two technologies.
  • Built-in replication (one of its latest and greatest additions, apparently).
  • Strong community support.
  • Strong Python support.
  • PLV8 seems cool.
  • Schemaless SQL using hstore, anyone?

There are lots of intricate details I didn't go over purposely. Just kinda running through this.

Set Up Postgres on Windows

Download the appropriate Postgres installer for your environment. Find the downloads page here.

Use the installer to install Postgres.

Set Up Psycopg2

  1. Psycopg is going to be our path to Postgres freedom with Python. It seems to be the most common way to interact with Postgres.
  2. Go to your command prompt.
  3. If you have your environment variables set up nicely and can run things like pip and easy_install, that's good. If not, locate pip and do the following
pip install -U psycopg2

If that fails (it did for me), download the appropriate installer for your environment here.

4. Install psycopg2 using the installer. 

Prepare a New Postgres Database for Your Project

  1. Go to where Postgres was installed after installing with the Windows installer.
  2. Locate pgAdminIII. This is most likely something one could easily find in their start menu. Just look for it.
  3. Open pgAdminIII.
  4. Draw your attention to your current databases in the tree located on the right of your window. Look for "Databases." The icon looks like a stack of pancakes.
  5. Right-click "Databases."
  6. Click "New Database..."
  7. Think up some name for your database. This will be important for setting Postgres up with Django.
  8. The "Owner" field should be filled out with a username. It'll be used in conjunction with your master password (the one you were asked to establish during install) when you set up Django.
  9. Click "OK."

Set up your Django Environment for Working With Postgres

  1. Your Django project should already be started/created at this point. If not, go to your command prompt (I'm assuming you set your environment variables to work with Django natively) and type
 django-admin startproject some_project_name
  1. Go to your main project folder (a sub folder of your project folder; it contains

  2. Locate and open it.

I'm only focused on hooking up the database. It's assumed you know how to set up everything else in your settings file.

Set up so that the database handling section (near the very beginning of the file) looks like this but with your own login credentials for the database:

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'testdb',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': 'foobarguy',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.


Save after you've done all of the configuration. You should be able to use Postgres with Django now.


I love you, Renee. I love you so much. In everything I do.


Software Engineer

Subscribe to GregBlogs