'testing.postgresql can't locate initdb command, even when postgress is installed and running

I have the following code:

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

My code runs perfectly when mode == 'prod' but, when mode == 'test', I get this error:

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

I can say that progress is installed and running, and C:\Program Files\PostgreSQL\13\bin is in PATH.

I can't find what I can be missing.



Solution 1:[1]

Ran into this trying to solve this issue myself. On macOS, I was able to fix this by initializing the locate db in a terminal with sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp. Run this and wait 10-15 minutes, and then restart the terminal and try locate initdb, it should return a list of locations. Then, make sure psql is in path with which psql, and even to be safe run which initdb. If all of these return locations, try your code again with a new terminal and it should work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sami-amer