Psycopg2 image not found
Asked Answered
N

23

83

Trying to setup postgres with the postgres mac app and hit this error, which I haven't been able to solve. Any thoughts?

    ImportError: dlopen(/Users/Craig/pyenv/mysite/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @executable_path/../lib/libssl.1.0.0.dylib
  Referenced from: /Applications/Postgres.app/Contents/MacOS/lib/libpq.dylib
  Reason: image not found
Neoplasm answered 6/5, 2013 at 22:0 Comment(0)
Z
90
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libssl.1.0.0.dylib /usr/lib
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libcrypto.1.0.0.dylib /usr/lib

I encountered this error while working on Django. I have it working on virtualenv with Django==1.3 but not on Django==1.5 where I have to issue the commands above.

In OS X El Capitan you can't do these links without disabling system protection but it works well if you link to /usr/local/lib

Zoltai answered 19/5, 2013 at 11:21 Comment(8)
This answer was the only one that worked for me that didn't screw up other systems like Mercurial etc. Looks like it's a known issue: github.com/PostgresApp/PostgresApp/issues/…Dunaway
And for those of you using Heroku's Postgres.app -- sudo ln -s /Applications/Postgres.app/Contents/MacOS/lib/libssl.1.0.0.dylib /usr/lib sudo ln -s /Applications/Postgres.app/Contents/MacOS/lib/libcrypto.1.0.0.dylib /usr/libGroce
In case you've already created a symbolic link from a previous version, be sure to remove it before you create a new one. sudo rm /usr/lib/libssl.X.X.X.dylib sudo rm /usr/lib/libcrypto.X.X.X.dylib Then add the new links following philippinedev answer. (Be sure to have the right version of files in your path of course)Kindhearted
I symlink into /usr/local/lib just because it feels safer and less likely to mess with the system than /usr/lib. Works just as well.Outrelief
Just a reminder that when you create the symlinks, you have to explicitly define the path to the file you're linking to. I forgot this, and spent forever trying to figure out why I was getting weird errors in psycopg2. The reason was that I had just cd'ed into the directory with libssl, and typed './libssl.1.0.0...'. This created the symlink to the literal string ./libssl.1.0.0..., which is obviously not what you want.Designer
@JoeShaw's comment is mandatory in OS X El Capitan. You'll get a ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted error otherwise. This is due to "rootless" mode. More info here: apple.stackexchange.com/questions/196224/…Grimbald
Can confirm sudo ln -s /Applications/Postgres.app/Contents/Versions/9.5/lib/libcrypto.1.0.0.dylib /usr/local/lib; sudo ln -s /Applications/Postgres.app/Contents/Versions/9.5/lib/libssl.1.0.0.dylib /usr/local/lib works for 9.5 Postgres.appRina
@Zoltai I get ImportError: dlopen(/Users/me/Desktop/foo/bar/one/blue/venv/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: @rpath/libssl.1.0.0.dylib . Any advice?Disdain
S
53
pip install psycopg2-binary

works like a charm!

Discussion on source+wheel distribution (pyscopg2) vs a separate binary distribution (psycopg2-binary): https://www.postgresql.org/message-id/CA%2Bmi_8bd6kJHLTGkuyHSnqcgDrJ1uHgQWvXCKQFD3tPQBUa2Bw%40mail.gmail.com

Explanation on the decision to release psycopg2-binary: http://initd.org/psycopg/articles/2018/02/08/psycopg-274-released/

However, there are reports of psycopg2-binary not satisfying psycopg2 dependencies. More discussion here: https://github.com/psycopg/psycopg2/issues/674

Synchromesh answered 19/7, 2019 at 16:3 Comment(1)
When answering an older question with existing answers it is useful to explain what differentiates your answer from the existing answers. For really old questions it is also useful to indicate if the answer relies on newer versions of the software.Ashelman
T
18

I found a solution that worked for me when dealing with a similar issue on rails. Add the following to your .bash_profile, .bash_rc, or equivalent:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/MacOS/lib:$DYLD_LIBRARY_PATH

(Assuming you installed Postgres.app in the default location). Then restart your terminal session and try again.

Exporting to DYLD_LIBRARY_PATH directly can cause serious problems with other apps that depend on it, but using the fallback path avoids these problems.

See also: https://github.com/PostgresApp/PostgresApp/issues/109#issuecomment-18387546

EDIT: It seems that setting DYLD_FALLBACK_LIBRARY_PATH causes an error when you try to run psql. To fix this, you can add the following two lines to your .bash_profile:

alias psql="(. ~/.bash_profile; unset DYLD_FALLBACK_LIBRARY_PATH; psql)";

This is assuming that you're using bash and that your .bash_profile is located in your home directory. If that's not the case (or if you're using a .bashrc or other environment setup instead of .bash_profile) change the ~/.bash_profile part of the command to the path to your environment setup script.

The aliased command basically starts a subshell which does not effect your current bash environment. So when it unsets the DYLD_FALLBACK_LIBRARY_PATH variable, it's only temporary. After you exit psql the environment variable will be set again.

Tremayne answered 24/5, 2013 at 17:28 Comment(1)
Minor tweaks for a virtualenv; I added this line to my postactivate: export "DYLD_FALLBACK_LIBRARY_PATH=/Library/PostgreSQL/9.4/lib:$DYLD_LIBRARY_PATH", and when installing used this command: DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.4/lib pip install psycopg2Scuba
L
17

This happened to me after upgrading Postgresql, and after installing psycopg2 in my virtualenv. Reinstalling (re-building) worked for me.

pip uninstall psycopg2
pip install psycopg2
Lien answered 9/9, 2015 at 18:51 Comment(3)
This worked for me after upgrading postgresql.app version 9.3 to 9.5 on OS X El Capitan 10.11.3.Bolitho
This worked for me after upgrading from Postgres 9.6 to 12.1. Thank you!Strawworm
Worked for me once I cleaned pip cache (pip cache purge) - the built version in cache was still pointing to the old libssl path.Top
B
11

This problem cost me the whole morning to solve. I found the discussion on http://initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os-x/ really helpful. Thanks to Jurie's answer, the solution to my problem (in Mac) is as below:

  1. Install openssl 1.0.0 using brew:

     brew install openssl
    
  2. using the following command:

    export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/**1.0.1x**/lib
    

    replace 1.0.1x part with your current version. For me it is 1.0.1h.

Hope this helps!

EDIT: After one day, I found that the second command has to be entered every time when needing to connect to database, so not a permanent solution to this problem.

Bottomry answered 30/7, 2014 at 17:13 Comment(4)
Setting the DYLD_LIBRARY_PATH environment variable when executing the command also works: DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.2d_1/lib/ python manage.py migrateParvenu
add export DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/**1.0.1x**/lib to your environment activate script (e.g. ~/anaconda3/envs/myenv/bin/activate)Satiny
you can also add the command in ~/.bash_profile and it will work everytime without having to enter it again and againZayin
@PetrosKyriakou, this would cause ssl import failure as can be seen from this issue: groups.google.com/a/continuum.io/forum/#!topic/anaconda/…Bottomry
M
10

In your bash environment before you load it, try this:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/x.y/lib

..replacing the 'x.y' with the version on your system.

..be aware that setting this in your bash profile can interfere with other programs, as KindOfGuy noted.

..of course, if you're not running it from a bash prompt, you'll have to set up your environment in whatever way pyenv lets you. ..you could even edit pyenv itself and place that at the top.

Another alternative is to put this in a python script which runs before you attempt to import psycopg2:

import os
os.environ['DYLD_LIBRARY_PATH'] = '/Library/PostgreSQL/x.y/lib'

..again, replacing 'x.y' with the version on your system in /Library/PostgreSQL.

Maguire answered 6/5, 2013 at 22:48 Comment(3)
Nothing... Though I checked out that folder /Library/PostgreSQL/9.2/ and the only item inside was a locked folder named 'data'. I imagine there should be other items, right?Neoplasm
running the export from bash profile caused trouble elsewhere, but I stuck os.environ['DYLD_LIBRARY_PATH'] = '/Library/PostgreSQL/x.y/lib' into manage.py which worked a charm. Will still need to do this in all projects, but this will help me until the issue gets sorted some other way.Piranha
On behalf of ctbailey : Here's an updated path for version 9.3 of Postgres.app : export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/Versions/9.3/lib:$DYLD_LIBRARY_PATH.Agripinaagrippa
C
7

I encountered this issue when I upgraded from postgres.app 9.4 to 9.5 on el capitan.

The other solutions will not work (easily) in el capitan because of the system lock on certain directories, meaning the symbolic link solution will be less accessible/ideal for most.

This leaves the fallback variable. The current answer points to the wrong directory. I am guessing the dylibs directory has changed since 2013.

So, here is the latest fallback directory that works for me:

export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Postgres.app/Contents/Versions/latest/lib:$DYLD_LIBRARY_PATH
Ceresin answered 10/1, 2016 at 0:13 Comment(1)
Thank you! A similar issue to that in the question was driving me absolutely batty, but prepending Postgres' lib to DYLD_FALLBACK_LIBRARY_PATH did the trick.Geist
L
4

Under Mac OS X 10.11 (El Capitan), /usr/lib is read only for root user. You'll get a ln: /usr/lib/libssl.1.0.0.dylib: Operation not permitted error You need to use /usr/local/lib instead of /usr.

$ sudo ln -s /Library/PostgreSQL/9.2/lib/libssl.1.0.0.dylib /usr/local/lib
$ sudo ln -s /Library/PostgreSQL/9.2/lib/libcrypto.1.0.0.dylib /usr/local/lib
Landers answered 12/3, 2016 at 20:7 Comment(0)
B
4

I encountered a similar problem I am using macOS executing pip install psycopg2-binary did wonder for me :)

Bostick answered 24/2, 2021 at 8:0 Comment(0)
D
3

Big picture, the problem is that a required library couldn’t be found. You can alter where psycopg2 looks for libssl using Apple’s open source compiler tools, otool and install_name_tool. These ship with OS X, and manual pages are available with man <command>.

Change into the psycopg2 module directory mentioned in the error message. Once there:

$ otool -L _psycopg.so
    ...
    @executable_path/../lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
    ...

This lists the libraries _psycopg2.so will look for. You can change where it’ll look with install_name_tool:

$ install_name_tool -change @executable_path/../lib/libssl.1.0.0.dylib /usr/local/opt/openssl/lib/libssl.1.0.0.dylib _psycopg.so

You’ll need to adjust for where you have libssl.1.0.0.dylib, of course. The example I gave is the default Homebrew path, but you might have it from Anaconda and/or the PostgreSQL app bundle. (brew install openssl if you don’t have it yet.) You’ll also likely need to repeat for libcrypto.

Executing this change may fail depending on how _psycopg2.so was built. If that happens, you could probably build the module yourself with custom library paths, but I won’t get into that.

This approach has the advantage of being narrower, and thus less risky, than the approach (given in other answers here) of linking libssl 1.0.0 into dyld’s search paths (either through ln -s or setting a DYLD_* environment variable). (See warnings against these approaches in a pair of discussions and some code. Learn more about dyld through man dyld.) It has the disadvantage of needing to be repeated for each copy of psycopg2. Choose your own adventure.

Disclaimer: Most of the content in this answer is from knowledge I cobbled together in one day. I am no expert.

Diaconicon answered 4/7, 2015 at 15:30 Comment(0)
G
2

I found this solution that worked for me

sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libssl.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libssl.1.0.0.dylib /usr/lib/libssl.dylib 

sudo cp /Applications/Postgres.app/Contents/Versions/9.3/lib/libcrypto.1.0.0.dylib /usr/lib
sudo ln -fs /usr/lib/libcrypto.1.0.0.dylib /usr/lib/libcrypto.dylib 

Replace this part '/Applications/Postgres.app/Contents/Versions/9.3/' depending upon the location where psql is installed on your machine. Command to find where psql is installed :which psql

UPDATE FROM COMMENTS: On OSX 10.11 (El Capitan), you can no longer copy files to /usr/lib. Use /usr/local/lib

Geerts answered 28/10, 2014 at 13:23 Comment(2)
Re: @MitMehta's solution: On OSX 10.11 (El Capitan), you can no longer copy files to /usr/lib. Use /usr/local/lib.Afternoon
Why not use the available 'latest' symlink in Postgres.app and copy to both /usr/lib and /usr/local/lib like: sudo ln -sf /Applications/Postgres.app/Contents/Versions/latest/lib/libssl.dylib {/usr/lib,/usr/local/lib}; sudo ln -sf /Applications/Postgres.app/Contents/Versions/latest/lib/libcrypto.dylib {/usr/lib,/usr/local/lib}Kitti
D
2

I was missing the postgresql client package, so I installed them with brew and that fixed this issue for me.

brew update
brew doctor
brew install postgresql
Delgado answered 11/11, 2014 at 17:12 Comment(0)
Z
2

I had the same error because of upgrading Postgres.app (from 9.3 to 9.4).

The solution was to delete the pip cache wheels because they were pointing to Postgres.app version 9.3.

I've found the according psycopg wheel cache files this way

grep -r psycopg ~/.pip/cache

and deleted the directory I've found with the last command.

Zimmerman answered 9/8, 2015 at 19:12 Comment(0)
S
2

The solution that worked for me was to install psycopg2 with the "no-binary" option set, which tells psycopg2 to use the system libssl.

By default psycopg2 uses its own version of libssl and it seemed to keep a dependency on an old version of the library which no longer existed after my upgrade.

See the psycopg2 install docs for more detail about the no-binary option.

pip uninstall psycopg2
pip install --no-binary :all: psycopg2
Statued answered 17/8, 2017 at 16:50 Comment(0)
A
2

In case if you are working on python/django with virtual environment

Worked for me for the following Version

  • MacOS Catalina 10.5.2
  • python 3.7.3
  • Django 3.0.2
  • psycopg2==2.8.4
  • virtualenv 16.6.0
  • pip 19.3.1
  • postgres (PostgreSQL) 11.2

3 Steps Solutions

  • Delete Existing Virtual Environment
  • Create New Virtual Environemnt again
  • Install Dependencies again
Appointment answered 10/1, 2020 at 9:22 Comment(0)
M
0

Did you install psycopg2? You need it in order to integrate python with postgresql.

Microhenry answered 6/5, 2013 at 22:3 Comment(1)
Yup, it's installed. I'm running a pyenv, might that affect things?Neoplasm
E
0

I'm experiencing a similar issue with mysql. The library requested is is failing to load /usr/local/mysql/lib/libmysqlclient_r.16.dylib

However, in this directory, there is a later version of the library: /usr/local/mysql/lib/libmysqlclient.20.dylib

I am working in a virtualenvwrapper and have attempted to reinstall all of the previously install dependencies to no avail. Any assistance would be greatly appreciated.

Embarrassment answered 31/3, 2017 at 18:58 Comment(0)
R
0

Just wanted to share what worked for me. I was using Anaconda with jupyter. The following did work:

 DYLD_LIBRARY_PATH=/usr/local/Cellar/openssl/1.0.2k/lib  jupyter notebook

But then caused later issues importing another library (plotly). After playing around with pip, I realised I should probably be using conda instead and the following seemed to fix everything.

conda install psycopg2
Rochet answered 16/5, 2017 at 10:55 Comment(0)
I
0

Try updating psycopg2 using pip install psycopg2 --upgrade to the latest version, then try again.

Intervene answered 6/3, 2018 at 12:22 Comment(0)
E
0

try finding the file or files libssl.1.0.0.dylib and copy them to your '/usr/local/lib/' for me the origin was, '/Library/PostgreSQL/11/lib/' ,(change the version to yours) alternatively you may copy all to '/usr/local/lib/' go to the origin and type: sudo cp * /usr/local/lib/ to copy all files. I found the solution and changed it to solve mine https://oscarvalles.wordpress.com/2017/03/24/dlopenlibrary-not-loaded-libssl-dylib-fail-to-import-psycopg2-in-virtualenv/

Exhume answered 4/7, 2019 at 9:37 Comment(0)
A
0

Use psycopg2-binary instead

pip install psycopg2-binary
Aedile answered 4/9, 2019 at 22:47 Comment(0)
B
0

I was encountering similar problem while running my Django server with postgreSQL as DB. I fixed it by relying on following steps, I am making use of conda to take care of virtual enviroments.

Create a new virtual enviroment I named it newenv

1 - conda create --name newenv

2 - conda activate newenv

3 - pip install -r requirements.txt

4 - python manage.py runserver

Afterwards It worked like a charm :).

Bostick answered 3/2, 2021 at 23:30 Comment(0)
B
0

I had same problem because when I built lambda function for AWS with pip install psycopg2-binary -t . on my computer where I had locally installed python3.8 but for lambda on AWS I chose python3.9. When I switch to same version all started to work again.

Brote answered 28/3, 2023 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.