Django points to the wrong version of Postgres

Question:

I downgraded Postgres.app from 9.6 to 9.5 by removing the Postgres.app desktop app. I updated the database by doing

(I downloaded Postgres by downloading Postgres.app Desktop app and I installed Django by doing pip install Django)

sudo /usr/libexec/locate.updatedb

And it looks like it is initiating database from the right directory.

/Applications/Postgres.app/Contents/Versions/9.5/bin/initdb
/Applications/Postgres.app/Contents/Versions/9.5/share/doc/postgresql/html/app-initdb.html
/Applications/Postgres.app/Contents/Versions/9.5/share/man/man1/initdb.1

However, when I am trying to do a migration in my Django app, it looks like the path is still point to the 9.6 version of Postgress

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/tenant_schemas/models.py", line 4, in <module>
    from tenant_schemas.postgresql_backend.base import _check_schema_name
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/tenant_schemas/postgresql_backend/base.py", line 14, in <module>
    import psycopg2
  File "/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: /Applications/Postgres.app/Contents/Versions/9.6/lib/libpq.5.dylib
  Referenced from: /Users/me/Desktop/myapp/venv/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: image not found
Asked By: YAL

||

Answers:

I think that your problem is that the version of psycopg2 that is currently installed references the C postgres library that was bundled with your previous install of postgres (/Applications/Postgres.app/Contents/Versions/9.6/lib/libpq.5.dylib).

Try uninstalling and reinstalling psycopg2.

pip uninstall psycopg2
pip install psycopg2
Answered By: aumo

This solves the problem for me:

  1. uninstall your psycopg2

    pip uninstall psycopg2

  2. then do this

    pip –no-cache-dir install -U psycopg2

Answered By: YAL

It try to load libpq.5.dylib from the symlink /opt/homebrew/opt/postgresql/lib/libpq.5.dylib but found not file, so you need to update it:

# TODO: get this from the error, after "Library not loaded:"
SYMLINK_PATH="/Applications/Postgres.app/Contents/Versions/9.6/lib/libpq.5.dylib"

# TODO: find this in your machine. The version maybe different than mine
DESTINATION_PATH="/opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib"

sudo mv $SYMLINK_PATH $SYMLINK_PATH.old
sudo ln -s $DESTINATION_PATH $SYMLINK_PATH
Answered By: Terry Tú Nguyễn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.