我正在尝试创建一个fastAPI连接到PostgreSQL的应用程序postgresql一个是在运行测试时。这是我的连接代码:
import sqlalchemy
import testing.postgresql
from sqlalchemy.ext.declarative import declarative_base
from app.config import Settings
from databases import Database
from sqlalchemy import (
create_engine
)
settings = Settings()
if settings.DB_HOST == 'local':
with testing.postgresql.Postgresql() as postgresql:
engine = create_engine(postgresql.url())
else:
db_config = {
"drivername": "postgresql",
"host": settings.DB_HOST,
"username": settings.DB_USER,
"password": settings.DB_PASSWORD,
"port": settings.DB_PORT,
"database": settings.DB_DATABASE
}
uri = sqlalchemy.engine.url.URL(**db_config)
engine = create_engine(uri)
database = Database(str(engine.url))
问题是这段代码引发了这个异常:
appdbsession.py:13: in <module>
with testing.postgresql.Postgresql() as postgresql:
autostorevenvlibsite-packagestestingcommondatabase.py:92: in __init__
self.initialize()
autostorevenvlibsite-packagestestingpostgresql.py:50: in initialize
self.initdb = find_program('initdb', ['bin'])
autostorevenvlibsite-packagestestingpostgresql.py:144: in find_program
raise RuntimeError("command not found: %s" % name)
E RuntimeError: command not found: initdb
我发现了一些类似的答案,但它们都发生在Docker容器中,这不是我的情况,所以这些答案不适合我的情况。
如果我设置我的PATH使initdb不在其中,我会得到相同的错误。显然测试。postgresql依赖于你是否安装了postgresql的二进制文件并且可以找到。