在Django上设置postgresql和运行代码

本文指导如何在Django项目中配置postgresql数据库,详细步骤包括创建数据库、在settings.py中设置数据库信息,并进行数据库迁移及代码运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建postgresql数据库

ubuntuq@ubuntuq-HP-Compaq-6910p-RH241AV:~$ su postgres
密码: 
postgres@ubuntuq-HP-Compaq-6910p-RH241AV:/home/ubuntuq$ psql
psql (9.3.11)
Type "help" for help.
postgres=# CREATE DATABASE pol_gov_mon owner deploy;
CREATE DATABASE
postgres=#

在django中设置postgresql数据库信息

把PostgreSQL中的数据库信息放在django设置中
settings.py部分代码如下:

DATABASES = {
    'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'pol_gov_mon',
         'USER': 'deploy',
         'PASSWORD': 'XXX',
         'HOST': '127.0.0.1',
         'PORT': '5432',
         #'CONN_MAX_AGE': 5,
     }
}

在django中迁移数据库和运行代码

ubuntuq@ubuntuq-HP-Compaq-6910p-RH241AV:~/下载/wuran$ python manage.py syncdb
#迁移数据库
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table jiandu_gov_pol_air_result
Creating table jiandu_gov_pol_water_result
Creating table jiandu_gov_pol_mental_result
Creating table jiandu_gov_pol_danger_result

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'ubuntuq'): #可为空
Email address: #可为空
Password: 
Password (again): 
Error: Blank passwords aren't allowed.
Password: #填入密码
Password (again):#填入密码 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
ubuntuq@ubuntuq-HP-Compaq-6910p-RH241AV:~/下载/wuran$ python manage.py xinjiang getall
#测试运行代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值