win10下 django 使用 远程 postgresql 数据库

一、系统环境

 1、开发环境

    windows10
    python 3.8.5
    django 2.2

2、远程数据库

    centos7
    postgresql 10.0

二、配置

1、安装依赖组件

    pip3 install psycopg2

2、修改项目settings.py文件

# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mysite',
        'USER': 'postgres',
        'PASSWORD': '',
        'HOST': '10.0.49.253',
        'PORT': '5432',
    }
}

 3、修改centos7下postgresql配置

    修改centos7下postgresql配置以允许远程访问
    修改安装目录下/data/postgresql.conf文件
    

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;

    修改/data/pg_hba.conf
    #允许192.168.1.0/24  10.0.242.0/24两个网段的IP访问
    

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
#允许192.168.1.0/24  10.0.242.0/24两个网段的IP访问
host    all             postgres        192.168.1.0/24          trust
host    all             postgres        10.0.242.0/24           trust

4、重启数据库
     sudo  systemctl restart postgresql-10

5、手工创建数据mysite

    在远程终端上用命令行创建

    sudo -i -u postgres
    psql
    create database mysite;    

[root@Openstack-Pike-Repos ~]# sudo -i -u postgres
-bash-4.2$ psql
psql (10.11)
Type "help" for help.

postgres=# CREATE DATABASE mysite;

6、开发端验证 

 H:\python_site\mysite>  py manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

7、查看远程postgresql中数据表生成状况

mysite=# \d
                        List of relations
 Schema |               Name                |   Type   |  Owner   
--------+-----------------------------------+----------+----------
 public | auth_group                        | table    | postgres
 public | auth_group_id_seq                 | sequence | postgres
 public | auth_group_permissions            | table    | postgres
 public | auth_group_permissions_id_seq     | sequence | postgres
 public | auth_permission                   | table    | postgres
 public | auth_permission_id_seq            | sequence | postgres
 public | auth_user                         | table    | postgres
 public | auth_user_groups                  | table    | postgres
 public | auth_user_groups_id_seq           | sequence | postgres
 public | auth_user_id_seq                  | sequence | postgres
 public | auth_user_user_permissions        | table    | postgres
 public | auth_user_user_permissions_id_seq | sequence | postgres
 public | django_admin_log                  | table    | postgres
 public | django_admin_log_id_seq           | sequence | postgres
 public | django_content_type               | table    | postgres
 public | django_content_type_id_seq        | sequence | postgres
 public | django_migrations                 | table    | postgres
 public | django_migrations_id_seq          | sequence | postgres
 public | django_session                    | table    | postgres
(19 rows)

三、参考文章
https://docs.djangoproject.com/zh-hans/2.2/ref/databases/#third-party-notes
https://www.cnblogs.com/life512/p/13083326.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值