Django使用postgresql数据库


1)添加新的数据库用户

 create user <...> with password <...>;

2)创建数据库并指定所属用户

 create database django_test owner <...>;

3)Django数据库配置

注:python环境中需安装psycopg2-binary:pip install psycopg2-binary

 DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.postgresql_psycopg2',
 'USER': '...',
 'PASSWORD': '...',
 'HOST': '...ip',
 'PORT': '...',
 'NAME': '...'
 }
 }

4)修改postgresql配置文件,指定允许远程连接

 # vi /etc/postgresql/10/main/postgresql.conf
 listen_addresses = '*' # 运行远程连接

5)重启postgresql服务

 systemctl restart postgresql

6)允许任意用户从任意机器上以密码方式访问数据库

 # vi /etc/postgresql/10/main/pg_hba.conf
 host all all 0.0.0.0/0 md5

7)重启postgresql服务


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM