簡體   English   中英

testing.postgresql 不能initdb

[英]testing.postgresql can not initdb

我正在嘗試創建一個fastAPI應用程序,它在運行時連接到PostgreSQL數據庫,但在運行測試時連接到testing.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))

問題是這段代碼引發了這個異常:

app\db\session.py:13: in <module>
    with testing.postgresql.Postgresql() as postgresql:
autostorevenv\lib\site-packages\testing\common\database.py:92: in __init__
    self.initialize()
autostorevenv\lib\site-packages\testing\postgresql.py:50: in initialize
    self.initdb = find_program('initdb', ['bin'])
autostorevenv\lib\site-packages\testing\postgresql.py:144: in find_program
    raise RuntimeError("command not found: %s" % name)
E   RuntimeError: command not found: initdb

我找到了一些類似的答案,但它們都發生在 Docker 容器中,這不是我的情況,所以這些答案不適合我的情況。

如果我設置我的 PATH 以便 initdb 不在其中,我會得到同樣的錯誤。 顯然,testing.postgresql 取決於您安裝了 PostgreSQL 二進制文件並且可以找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM