For marketers
who love technology
Home » , » How to configure Django with PostgreSQL?

How to configure Django with PostgreSQL?

In this post, I will explain you how to configure Django with PostgreSQL instead of MySQL.

The blue/white elephant logo of the PostgreSQL...
I did several changes locally before deploying my app to heroku and Amazon S3.

I changed my configuration to use postgreSQL instead of mySQL. I did not have any data stored in MySQL, so I reinitiated a brand new database. To do that, I had to come back to django's tutorial and edit

        #'ENGINE': 'django.db.backends.mysql', # USE MYSQL
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # USE postgreSQL

Of course, I had to install PostgreSQL and to create the database, end user, and password referenced in my django according to the tutorial. The installation of psycopg2 through easy_install may lead to various errors such as the ones below:

ImportError: No module named psycopg2.extensions
Error: pg_config executable not found.
That's why we include several packages in the first apt-get install directive. These packages solve the above errors.

sudo apt-get update
sudo apt-get install postgresql postgresql-client python-psycopg2 libpq-dev  postgresql-server-dev-all python-dev python-psycopg2 --fix-missing
easy_install psycopg2
sudo adduser MYUSER
sudo su postgres
postgres=# CREATE DATABASE thenameofmydb;
postgres=# GRANT ALL PRIVILEGES ON DATABASE thenameofmydb to MYUSER;
postgres=# \q
sudo vim /etc/postgresql/8.4/main/pg_hba.conf

In this file, check the following configuration directive:

# "local" is for Unix domain socket connections only
local   all         all                               trust

Then, we restart psql and log in:

sudo /etc/init.d/postgresql reload
psql -d thenameofmydb -U MYUSER 

About Gilles


Post a Comment