A note on enabling PostgreSQL with Django

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

A note on enabling PostgreSQL with Django
Looking back on creating a web service with Django 1
Create a homepage with django
Looking back on creating a web service with Django 2
A note on speeding up Python code with Numba
Deploy a Django app made with PTVS on Azure
Launch Django on a Docker container with docker-compose up
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Deploy a Django application with Docker
Django Tips-Create a ranking site with Django-
Implement a Django app on Hy
Build a web application with Django
Make a filter with a django template
[Note] Run Django on Amazon Linux 2
Note: Send an email with Django
Create a file uploader with Django
Django note 4
A note on what you did to use Flycheck with Python
Django Note 5
Launched a web application on AWS with django and changed jobs
Django Note 1
Django note 3
Django note 2
A simple RSS reader made with Django
[Note] Get data from PostgreSQL with Python
Deploy Django api on heroku (personal note)
Django + Apache with mod_wsgi on Windows Server 2016
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Throw a request with a certificate on httpie
Creating a login screen with Django allauth
Execute SQL with Django + Postgresql (including PreparedStatement)
I made a WEB application with Django
Build Django + NGINX + PostgreSQL development environment with Docker
Try Tensorflow with a GPU instance on AWS
Build a Django environment on Raspberry Pi (MySQL)
Build a python environment with ansible on centos6
How to develop a cart app with Django
Start Django in a virtual environment with Pipenv
A note on customizing the dict list class
Internationalization with django
[Python] Build a Django development environment with Docker
[Note] Create a one-line timezone class with python
A note on optimizing blackbox functions in Python
Build a Django environment with Vagrant in 5 minutes
Create a dashboard for Network devices with Django!
Folium: Visualize data on a map with Python
Start a process with a scheduling policy on Linux
A story about implementing a login screen with django
Build a Django development environment with Doker Toolbox
Create a one-file hello world application with django
A note about checking modifiers with Max Plus
A simple to-do list created with Python + Django
What Python beginners got hooked on with Django
Configure a module with multiple files in Django
Mount a directory on another server with sshfs
Just a note
Quickly build a Python Django environment with IntelliJ
Decrypt a string encrypted on iOS with Python
Visualize grib2 on a map with python (matplotlib)