版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YuYan_wang/article/details/81204357
1、 修改 settings.py 文件中的数据库配置
# 默认配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改为
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb', # 数据库名字(需要先创建)
'USER': 'postgres', # 登录用户名
'PASSWORD': '123456', # 密码
'HOST': '', # 数据库IP地址,留空默认为localhost
'PORT': '5432', # 端口
}
}
2、配置服务器
# 由于 MySQLdb 模块还不支持 Python3.x,所以 Python3.x 如果想连接MySQL需要安装 PyMySQL 模块。
pip install PyMySQL
pip3 install PyMySQL # python3 版本以上
# 链接 PostgreSQL ,需要安装 psycopg2 模块
pip install psycopg2
# 创建migrations 文件包中迁移文件(文件名格式:0001_initial.py)
python manage.py makemigrations
# 迁移
python manage.py migrate
# 创建 超级管理员
python manage.py createsuperuser
# 出现 admin名称 自己起名, 邮箱地址可以直接回车忽略, 密码 自己起,超级管理员是用来登录Django admin后台使用的
3 、报错事项
Table ' *.django_session' doesn't exist .
这个问题是由于数据库没有表的原因
Django 1.9 以前的版本 使用python manage.py syncdb
Django 1.9 以后的版本 使用上面的创建和迁移 python manage.py migrate
Peer authentication failed for user "postgres"
Peer authentication 是默认的配置,如果你的计算机用户名和你的postgres数据库名是一样的话,那么就不会出现此错误,不需要为你的数据库设置密码。
还有一种md5 authentication,它需要密码。
而我的计算机用户名和我的数据库名不一致,所以需要把Peer authentication改成md5 authentication,然后给数据库设置密码。
/etc/postgresql/9.3/main/pg_hba.conf
找到下面的一行:
local all postgres peer
改成
local all postgres md5
然后restart postgresql server
安装PyMySQL 出现问题 安装不上 可以选择源码安装
$ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL/ $ python3 setup.py install