Centos7 + Python3.7.0 + Django + postgreSQL9.6 配置

按照前面的設置,假設我們現在已經安裝了 virtualenv, Django2/3, postgreSQL9.6.

1. 看下 django 的版本,如果是 版本2/3 的話,則適用本教程

python -c "import django; print(django.get_version())"

2. 安裝一些依賴包

yum install postgresql-libs

yum install python-devel postgresql-devel

上面的3個依賴包基本上安裝完就沒有問題了

3. 安裝 psycopg2

pip install psycopg2

4. root 權限下新建 postgreSQL 用戶,數據庫

su - postgres
psql
CREATE DATABASE wagtail;
CREATE USER wagtail WITH PASSWORD 'wagtail';
ALTER ROLE wagtail SET client_encoding TO 'utf8';
ALTER ROLE wagtail SET default_transaction_isolation TO 'read committed';
ALTER ROLE wagtail SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE wagtail TO wagtail;
\q

其中的 alter role 部分主要是基於 django 的建議

https://docs.djangoproject.com/en/3.0/ref/databases/#postgresql-notes

Optimizing PostgreSQL’s configuration¶
Django needs the following parameters for its database connections:

client_encoding: 'UTF8',
default_transaction_isolation: 'read committed' by default, or the value set in the connection options (see below),
timezone: 'UTC' when USE_TZ is True, value of TIME_ZONE otherwise.
If these parameters already have the correct values, Django won’t set them for every new connection, which improves performance slightly. You can configure them directly in postgresql.conf or more conveniently per database user with ALTER ROLE.

Django will work just fine without this optimization, but each new connection will do some additional queries to set these parameters.

5. 配值 django 設置文件 setting.py

        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'wagtail',
        'USER': 'wagtail',
        'PASSWORD': 'wagtail',
        'HOST': '127.0.0.1',
        'PORT': '5432',

6. 數據遷移, 啓動

python manage.py migrate

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章