I've been trying to create a connection to my postgres DB. By doing so, I get following error by running this code
import pandas as pd
import numpy as np
import os
from sqlalchemy import create_engine, text
from datetime import datetime, timedelta
from pytz import timezone
postgres = create_engine("postgresql+psycopg2://user:password@xx.xx.xx.xx", connect_args={'sslmode':'require'})
Following error message appears:
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
/User/Documents/github/new_project copy.py Cell 4 in <cell line: 8>()
5 from datetime import datetime, timedelta
6 from pytz import timezone
----> 8 postgres = create_engine("postgresql+psycopg2://user:password@xx.xx.xx.xx", connect_args={'sslmode':'require'})
File <string>:2, in create_engine(url, **kwargs)
File ~/Documents/github/new_project/.venv/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py:309, in deprecated_params.<locals>.decorate.<locals>.warned(fn, *args, **kwargs)
302 if m in kwargs:
303 _warn_with_version(
304 messages[m],
305 versions[m],
306 version_warnings[m],
307 stacklevel=3,
308 )
--> 309 return fn(*args, **kwargs)
File ~/Documents/github/new_project/.venv/lib/python3.9/site-packages/sqlalchemy/engine/create.py:560, in create_engine(url, **kwargs)
558 if k in kwargs:
559 dbapi_args[k] = pop_kwarg(k)
--> 560 dbapi = dialect_cls.dbapi(**dbapi_args)
562 dialect_args["dbapi"] = dbapi
564 dialect_args.setdefault("compiler_linting", compiler.NO_LINTING)
...
67 from psycopg2 import extensions as _ext
ImportError: dlopen(/Users/myname/Documents/github/new_project/.venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): Library not loaded: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib'
Referenced from: '/Users/myname/Documents/github/new_project/.venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so'
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)
(due to privacy reasons I changed the path, however, the logic was not changed)
Therefore, the error seems with the installation of the psycopg2
library. But by running the pip3 list I get psycopg2 2.9.3 showed as installed.
I'm running the code in a virtual environment (venv) but I also checked locally, which did not eliminate the problem.
Also to note is that this problem did not occur before. It could be that it is somehow related to the upgrade from postgresql@13 to postgresql@14, which I reversed again in the meantime. Additionally, I am running the code on a macOS with a M1 chip.
I looked up numerous articles in StackOverflow and other sites (Problems using psycopg2 on Mac OS (Yosemite), Import psycopg2 Library not loaded), but none of them could solve the problem.
As all my attempts failed for example reinstalling psycopg2 and also installing psycopg2-binary did not solve my problem, a possible solution or an indication of the source of the error is much appreciated.