【问题标题】:testing.postgresql can't locate initdb command, even when postgress is installed and runningtesting.postgresql 无法找到 initdb 命令,即使安装并运行了 postgres
【发布时间】:2022-03-18 11:52:19
【问题描述】:

我有以下代码:

import sqlalchemy
import testing.postgresql
from sqlalchemy.ext.declarative import declarative_base
from app.config import Settings, mode
from databases import Database

from sqlalchemy import (
    create_engine
)

def get_database():
    if mode == 'prod':
        settings = Settings()
        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)
        Base = declarative_base()
        database = Database(str(engine.url))

        return engine, Base, database
    else:
        with testing.postgresql.Postgresql() as postgresql:
            engine = create_engine(postgresql.url())
            Base = declarative_base()
            database = Database(str(engine.url))

            return engine, Base, database

engine, Base, database = get_database()

我的代码在mode == 'prod' 时运行完美,但在mode == 'test' 时,我收到此错误:

venv\lib\site-packages\testing\postgresql.py:144: in find_program
    raise RuntimeError("command not found: %s" % name)
E   RuntimeError: command not found: initdb

我可以说进度已安装并运行,C:\Program Files\PostgreSQL\13\binPATH 中。

我找不到我可能缺少的东西。

【问题讨论】:

    标签: python-3.x postgresql sqlalchemy


    【解决方案1】:

    自己尝试解决这个问题时遇到了这个问题。在 macOS 上,我可以通过使用 sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp 在终端中初始化定位数据库来解决此问题。运行这个并等待 10-15 分钟,然后重新启动终端并尝试locate initdb,它应该返回一个位置列表。然后,确保psqlwhich psql 在路径中,甚至为了安全运行which initdb。如果所有这些返回位置,请使用新终端再次尝试您的代码,它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-01-03
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2022-11-30
      • 2020-08-29
      相关资源
      最近更新 更多