一、配置方式
- 解压opengauss python驱动包
tar -zxvf openGauss-3.0.1-openEuler-x86 64Python.tar.gz -C /home/centos-python3
- psycopg2 拷贝到/usr/lib64/python3.7/site-packages/下面,权限改为755
cp -r psycopg2/ /usr/lib64/python3.7/site-packages
chmod -R 755 /usr/lib64/python3.7/site-packages/psycopg2/
- 设置用户环境变量
export LD LIBRARY PATH=/home/centos-python3/lib:$LD_LIBRARY_PATH
- 执行python3.7
python3.7
执行成功即配置成功
- 连接串配置
host=ip1,ip2 port=port1,port2 dbname=数据库名 user=用户名 password=密码 参数=xxx
示例:
host=172.198.194.194,172.198.194.195 port=26000,26000 dbname=postgres user=test password=startinit123 target session attrs=read-write
参数说明:
target_session_attrs=read-only : 默认情况下,会话不能接受任何读写事务
target_session_attrs=read-write: 默认情况下,可接受的读写事务中仅可以接受一个连接
二、测试程序
- 示例
#coding: utf-8
import psycopg2
# 连接数据库,根据情况修改连接串
conn = psycopg2.connect("host=172.198.194.194 172.198.194.195 port=26000,26000 dbname=postgres user=test password=startinit123 target_session_attrs=read-write")
# 获取游标
cur = conn.cursor()
#执行sql,替换一个你想执行的sql
cur.execute("insert into test values(1,'a');select count(*) from test;")
# 获取一条执行结果
print(cur.fetchone())
# 提交
conn.commit()
#关闭游标
cur.close()
# 关闭连接
conn.close()
- 执行
# python3.7 psycopg-test.py