简体   繁体   中英

testing.postgresql can not initdb

I am trying to create a fastAPI app that connects to a PostgreSQL database when running, but to a testing.postgresql one when running tests. This is my code for the connection:

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))

The problem is that this code raises this exception:

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

I have found some similar answers, but they all happen in Docker containers, which is not my case, so those answers are not good for my case.

I get the same error if I set my PATH so that initdb is not in it. Apparently testing.postgresql depends on you having the PostgreSQL binaries installed and findable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM