【问题标题】:testing.postgresql can not initdbtesting.postgresql 无法初始化数据库
【发布时间】:2021-09-27 22:22:30
【问题描述】:

我正在尝试创建一个 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 容器中,这不是我的情况,所以这些答案对我的情况不利。

【问题讨论】:

  • 请出示进口商品。
  • @jjanes 添加到问题正文中

标签: python postgresql testing sqlalchemy


【解决方案1】:

如果我将我的 PATH 设置为 initdb 不在其中,我会得到同样的错误。显然 testing.postgresql 取决于您是否安装了 PostgreSQL 二进制文件并且可以找到。

【讨论】:

  • 你的意思是 PostgreSQL 数据库二进制文件?
  • 是的。诸如 initdb、pg_ctl、postgres/postmaster 之类的东西。加上一些实用程序,如 pg_config、pg_controldata 等。
猜你喜欢
  • 2023-04-08
  • 2019-08-08
  • 1970-01-01
  • 2022-01-18
  • 2017-05-11
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多