crewAIInc/crewAI · error · ValueError

Database name is required in the URI

Error message

Database name is required in the URI

What it means

Thrown by PGFileLoader after URI parsing when the path component (which carries the database name) is missing or empty. The loader builds connection_params['database'] from parsed.path.lstrip('/'); a URI like postgresql://host:5432 with no trailing database yields None and trips this check.

Source

Thrown at lib/crewai-tools/src/crewai_tools/rag/loaders/postgres_loader.py:48

            raise ValueError("Database URI is required for PostgreSQL loader")

        query = source.source

        parsed = urlparse(db_uri)
        if parsed.scheme not in ["postgresql", "postgres", "postgresql+psycopg2"]:
            raise ValueError(f"Invalid PostgreSQL URI scheme: {parsed.scheme}")

        connection_params = {
            "host": parsed.hostname or "localhost",
            "port": parsed.port or 5432,
            "user": parsed.username,
            "password": parsed.password,
            "database": parsed.path.lstrip("/") if parsed.path else None,
            "cursor_factory": RealDictCursor,
        }

        if not connection_params["database"]:
            raise ValueError("Database name is required in the URI")

        try:
            connection = connect(**connection_params)
            try:
                with connection.cursor() as cursor:
                    cursor.execute(query)
                    rows = cursor.fetchall()

                    if not rows:
                        content = "No data found in the table"
                        return LoaderResult(
                            content=content,
                            metadata={"source": query, "row_count": 0},
                            doc_id=self.generate_doc_id(
                                source_ref=query, content=content
                            ),
                        )

View on GitHub (pinned to 754d7323be)

Solutions

  1. Append the database name to the URI path: postgresql://user:pass@host:5432/mydb.
  2. Verify the env var supplying the database name is set and non-empty before building the URI.
  3. If the URI is built by concatenation, log the final string (with credentials masked) once to confirm it ends with /<dbname>.
  4. Remember the loader only supports database selection via the URI — there is no separate database kwarg.

Example fix

# before
db_uri = f"postgresql://user:pass@host:5432{os.getenv('POSTGRES_DB', '')}"  # empty -> error

# after
db_name = os.environ["POSTGRES_DB"]  # fail fast if unset
db_uri = f"postgresql://user:pass@host:5432/{db_name}"
Defensive patterns

Strategy: validation

Validate before calling

from urllib.parse import urlparse

def has_database_name(uri: str) -> bool:
    path = urlparse(uri).path
    return bool(path.lstrip("/"))

Try / catch

try:
    result = pg_loader.load(src, metadata={"db_uri": uri})
except ValueError as e:
    if "Database name is required" in str(e):
        raise ConfigError(f"URI missing /dbname: {mask(uri)}") from e
    raise

Prevention

When it happens

Trigger: Passing postgresql://user:pass@localhost:5432 (no /dbname), or a URI ending in just '/' (empty path after lstrip). Common when the DSN is assembled by string concatenation and the database segment is omitted because the variable holding it is unset.

Common situations: Environment-specific configs where the DB name lives in a separate env var (POSTGRES_DB) that is not set locally; templates like postgresql://host:5432/${DB_NAME} with an empty interpolation; copying a healthcheck DSN that never needed a database name.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/2604441453a120bf. Report an issue: GitHub.