Django 配置postgresql数据库

来自linux中国网wiki
跳到导航 跳到搜索

在django中使用postgresql数据库

You need to install psycopg2 Python library.

Installation

Download http://initd.org/psycopg/, then install it under Python PATH
After downloading, easily extract the tarball and:
$ python setup.py install
Or if you wish, install it by either easy_install or pip.

(I prefer to use pip over easy_install for no reason.)
$ easy_install psycopg2
$ pip install psycopg2 


Configuration

postgresql Configuration

su - postgres
psql 

postgres=# create  database mydatabase TEMPLATE = template0   ENCODING = 'UTF8' ;

sudo vim /etc/postgresql/9.6/main/pg_hba.conf

host    all     all         127.0.0.1/32                         trust 

django Configuration

in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',                      
        'USER': 'db_user',
        'PASSWORD': 'db_user_password',
        'HOST': ,
        'PORT': 'db_port_number',
    }
}

#my setttings file
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'postgres',
        'PASSWORD': '2233333',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

mysql etc

mysqlclient是MySQLdb的一个分支,它与python3有着特别好的契合并且可以作为MySQLdb的直接替代。在书写这篇的时候,这是在Django使用MySQL的推荐的选择。 MySQL Connector/Python是一个来自Oracle的纯python驱动,它不需要MySQL client库或在标准库之外的任何Python模块。 所有这些驱动都是线程安全的,并提供连接池。MySQLdb是当前唯一一个不支持python3的

参考

Django 配置MySQL数据库 How to setup PostgreSQL Database in Django https://docs.djangoproject.com/en/1.8/ref/settings/#databases http://usyiyi.cn/documents/django_182/ref/databases.html#postgresql-notes How To Install and Configure Django with Postgres, Nginx, and Gunicorn