django连接postgresql

开启远程访问

vi /var/lib/pgsql/9.5/data/postgresql.conf
修改#listen_addresses = 'localhost'  为  listen_addresses='*'

 信任远程连接

vi /var/lib/pgsql/9.5/data/pg_hba.conf
修改如下内容,信任指定服务器连接
# IPv4 local connections:
host    all            all      127.0.0.1/32      ident
host    all            all      223.98.169.0/24(需要连接的服务器IP)  trust

 pg_hba.conf


在该配置文件的host all all 127.0.0.1/32 md5行下添加以下配置,或者直接将这一行修改为以下配置


host all all 0.0.0.0/0 md5


如果不希望允许所有IP远程访问,则可以将上述配置项中的0.0.0.0设定为特定的IP值。

 Django中配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'wei',
        'USER':'postgres',
        'PASSWORD':'123123',
        'HOST':'106.13.104.194',
        'PORT':5432
    }

 测试

import psycopg2
conn = psycopg2.connect(database="wei", user="postgres", password="123123", host="106.13.104.194", port="5432")
print  ("Opened database successfully")
import  csv
import psycopg2
conn = psycopg2.connect(database="bms", user="postgres", password="123456", host="192.168.99.200", port="5432")
cursor=conn.cursor()
sql = """
      COPY  tb_1140b07706754f6fabae114537974d87      TO  '/home/tb_1140b07706754f6fabae114537974d87.csv' ;
"""
print(sql)
cursor.execute(sql)
ret =cursor.fetchall()

  

I can feel you forgetting me。。 有一种默契叫做我不理你,你就不理我

原文地址:https://www.cnblogs.com/weidaijie/p/10613254.html