Search code examples
postgresqlflasksqlalchemypostgresql-9.3pgadmin-4

Issue with connecting Postgresql with flask


While I was trying to connect my flask app with postgresql I was getting the following error which says I don't have the libpq.5 the error log as follows

Traceback (most recent call last): File "/Users/noelsjacob/Desktop/Projects/pgflask/main.py", line 3, in app = create_app() ^^^^^^^^^^^^ File "/Users/noelsjacob/Desktop/Projects/pgflask/Mautourco/init.py", line 10, in create_app db.init_app(app) File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/flask_sqlalchemy/extension.py", line 326, in init_app engines[key] = self._make_engine(key, options, app) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/flask_sqlalchemy/extension.py", line 614, in _make_engine return sa.engine_from_config(options, prefix="") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 804, in engine_from_config return create_engine(url, **options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 2, in create_engine File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/sqlalchemy/util/deprecations.py", line 283, in warned return fn(*args, **kwargs) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^ File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/sqlalchemy/engine/create.py", line 601, in create_engine dbapi = dbapi_meth(**dbapi_args) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 669, in import_dbapi import psycopg2 File "/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/psycopg2/init.py", line 51, in from psycopg2._psycopg import ( # noqa ImportError: dlopen(/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so, 0x0002): Library not loaded: '@rpath/libpq.5.dylib' Referenced from: '/Users/noelsjacob/.local/share/virtualenvs/pgflask-3P3nCHJ0/lib/python3.11/site-packages/psycopg2/_psycopg.cpython-311-darwin.so' Reason: tried: '/opt/homebrew/lib/libpq.5.dylib' (no such file), '/opt/homebrew/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file


Solution

  • The libpq.5.dylib library is a part of the PostgreSQL database, it needs to be installed on your system before you can use psycopg2 to connect to the database.

    First, make sure that PostgreSQL is installed on your system. If you're using Mac, you can install it using Homebrew:

    brew install postgresql
    

    In case PostgreSQL is already installed on your machine, it might be that libpq.5.dylib library is not in your system's library path. So make sure DYLD_LIBRARY_PATH variable is set:

    export DYLD_LIBRARY_PATH="/usr/local/Cellar/postgresql/<version>/lib/postgresql/:$DYLD_LIBRARY_PATH"