0%

Django 数据库设置

引子

Django 数据库设置

环境

  • centos7
  • Django 1.11

目的

记录Django 各种数据库的配置及简单用法

Sqlite 3

Django 原生支持

修改setting.py

1
2
3
4
5
6
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

Postgresql

安装依赖包和数据库软件

1
2
$ sudo yum install python-devel postgresql-devel
$ pip install psycopg2

修改setting.py

1
2
3
4
5
6
7
8
9
10
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'testdb',
'USER': 'postgres',
'PASSWORD':'postgres'
'HOST': 'db',
'PORT': '5432',
}
}

测试

1
2
3
python manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor

参考 Postgresql-first 那篇文字

欢迎关注我的其它发布渠道