Install psycopg2

本文指导如何检查Python版本,更新pip,设置PostgreSQL路径,并解决psycopg2安装中可能遇到的问题,包括依赖openssl的安装与环境变量配置。此外,提供了安装psycopg2的常规和二进制方式。
摘要由CSDN通过智能技术生成
  • Make sure you have Python 3 version between 3.4 to 3.7. You can find out with
$ python --version
  • Use the latest pip version:
$ pip3 install -U pip
  • Replace X.Y in the export PATH… line with the version of Postgres you are using. Find out with $ postgres -V. E.g.:
$ postgres -V
postgres (PostgreSQL) 10.2

If the version is 10.2, then replace the X.Y in the export PATH line with 10.2:
In ~/.bash_profile or ~/.bashrc, we should add:

export PATH=/usr/lib/postgresql/10.2/bin/:$PATH
  • To export and add things to your PATH, add the export PATH=… line to either ~/.bashrc or ~/.bash_profile on your machine.
$ vim ~/.bashrc`

# or 

$ vim ~/.bash_profile

where you can use :w, :wq vim commands to edit your bash file and add the export PATH=… line somewhere.

  • When you are done editing your bash profile, be sure to run source ~./bash_profile or source ~/.bashrc on your edited file, so your terminal session can grab the latest profile changes.
  • After editing your bash profile, you are ready to run the install step:
$ pip install psycopg2
  • A prerequisite for psycopg2 is OpenSSL. If you try installing and run into error ld: library not found for -lssl, then install openssl first.
    • On homebrew (for macOS or Linux): run brew install openssl (or sudo brew install openssl)
    • Add the LIBRARY_PATH to your bash profile:
    export 	LIBRARY_PATH = $LIBRARY_PATH:/usr/local/opt/openssl/lib/
    
    Don’t forget to run source ~/.bash_profile or source ~/.profile when done.
  • If the regular install doesn’t work, you can also just install the binary version instead:
pip install psycopg2-binary

which replaces the need to run pip install psycopg2

可以用这个命令来判断psycopg2 version

python -c "import psycopg2; print(psycopg2.__version__)"
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值