0
点赞
收藏
分享

微信扫一扫

python如何连接openGauss及django相关配置

张宏涛心理 04-15 07:30 阅读 1

前言

网络上很多类似教程,但是有可能不适用。这里给出官网的教程当作参考

网络上的方案

安装psycopg2包。
pip install psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装完成后,导入包即可使用

import psycopg2

# Connect to your postgres DB
conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a query
cur.execute("SELECT * FROM my_data")

# Retrieve query results
records = cur.fetchall()

但是系统有可能会缺少libpq-dev库。安装后,仍然提示缺少libpq库。
所以官网给出第二种方案。

官网方案

在这里插入图片描述

使用官网方式安装:
pip install psycopg2-binary

官网给出了例子,且给出了2种方式的对比。

django配置连接openGauss

openGauss:主要是把驱动的类路径改一下
DATABASES = {
    'default': {
        'ATOMIC_REQUESTS': True,
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': dbName,  
        'USER': dbUser,  
        'PASSWORD': dbPasswd,  
        'HOST': dbHost, 
        'PORT': dbPort,
    }
}


对比mysql
mysql:
DATABASES = {
    'default': {
        'ATOMIC_REQUESTS': True,
        'ENGINE': 'django.db.backends.mysql',
        'NAME': dbName,  # 替换为自己的数据库名,请预先创建好编码为utf8mb4的数据库
        'USER': dbUser,  # 数据库用户名
        'PASSWORD': dbPasswd,  # 数据库密码
        'HOST': dbHost,  # 数据库地址
        'PORT': dbPort,
    }
}



举报

相关推荐

0 条评论