连接PostgreSQL与Flask时出现问题。

huangapple go评论56阅读模式
英文:

Issue with connecting Postgresql with flask

问题

I will only translate the non-code portion of your text:

当我尝试将我的Flask应用连接到PostgreSQL时,我遇到了以下错误,提示我没有libpq.5。错误日志如下:

> Traceback (most recent call last):
File "/Users/noelsjacob/Desktop/Projects/pgflask/main.py", line 3, in <module>
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 "<string>", 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 <module>
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

英文:

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 <module>
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 "<string>", 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 <module>
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

答案1

得分: 1

libpq.5.dylib库是PostgreSQL数据库的一部分,在使用psycopg2连接数据库之前,需要在您的系统上安装它。

首先确保您的系统上安装了PostgreSQL。
如果您使用的是Mac,您可以使用Homebrew安装它:

brew install postgresql

如果PostgreSQL已经安装在您的机器上,可能是因为libpq.5.dylib库不在您的系统库路径中。因此,请确保设置DYLD_LIBRARY_PATH变量:

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

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=&quot;/usr/local/Cellar/postgresql/&lt;version&gt;/lib/postgresql/:$DYLD_LIBRARY_PATH&quot;

huangapple
  • 本文由 发表于 2023年5月7日 01:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定