说明
之前python的项目使用的mysql,近期要切换到国产数据库OpenGauss。
之前的方案是fastapi+sqlalchemy,测试下来发现不用改代码,只要改下配置即可。
切换方案
安装openGauss-connector-python-psycopg2
其代码工程在:https://gitee.com/opengauss/openGauss-connector-python-psycopg2#%E5%AE%89%E8%A3%85-psycopg2
也可以直接下载华为编译好的包,下载网页在:https://opengauss.org/zh/download/。解压安装包后,会得到两个目录 lib 和 psycopg2。
# 通过如下指令来查找 site-packages 目录所在位置:
python -c "from distutils.sysconfig import get_python_lib;print(get_python_lib())"
#输出/usr/local/lib/python3.9/site-packages
#将 psycopg2 文件夹整个拷贝该目录下,并授权便于其他用户使用
cp -r psycopg2 /usr/local/lib/python3.9/site-packages/
chmod 755 /usr/local/lib/python3.9/site-packages/psycopg2
# 将解压得到的lib目录加到LD_LIBRARY_PATH中
echo "export LD_LIBRARY_PATH=/path/to/lib/:$LD_LIBRARY_PATH" >> ~/.bashrc
source ~/.bashrc
若不需要使用autoescape相关功能,则不用安装openGauss-connector-python-psycopg2,直接装psycopg2-binary就行:
pip install psycopg2-binary
安装opengauss-sqlalchemy
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
python -m pip install opengauss-sqlalchemy
使用新URL
DATABASE_URI=opengauss://mygaussdbusername:passwd0123@10.xx.xx.xx:9xxx/mybasename