Environment: Debian8.2, Python3.4 (built on virtualenv)
** 1, Django installation / new project creation **
 pip install Django
 django-admin startproject hoge
** 2, PostgreSQL installation **
 apt-get install postgresql
** 3, install libpq-dev ** (Libpq is an interface for PostgreSQL written in C language. An engine for various application interfaces, without it I can't use PostgreSQL from Python)
apt-get install libpq-dev
** 4, Install psycopg2 ** (Psycopg2 is a PostgreSQL adapter for Python. Without it, the following is omitted)
apt-get install python-psycopg2
pip install psycopg2
** 5, Create new user and password in PostgreSQL ** (Log in to PostgreSQL as superuser and Create a database and user for your Django project)
su - postgres
psql (Login to PostgreSQL interactive mode as superuser)
CREATE ROLE testuser WITH PASSWORD'testpasswd'; (Create user)
CREATE DATABASE fuga OWNER testuser ENCODING'UTF8'; (Create database)
For more information on PostgreSQL commands https://www.postgresql.jp/document/9.4/html/sql-commands.html
** 6, write database settings to Django config file ** In settings.py in the hoge project created in 1, Change database settings as below
settings.py
DATABASES = {
    'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'fuga',
         'USER': 'testuser',
         'PASSWORD' : 'testpasswd',
         'HOST' : '127.0.0.1',
         'PORT' : 5432,
     }
 }
** 7, Perform migration **
python manage.py migrate
Is executed, and if no error occurs, it is OK.
Recommended Posts