Install psycopg2

  • 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__)"

Guess you like

Origin blog.csdn.net/BSCHN123/article/details/121189286
Recommended