說明
之前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