{"record":{"id":"2604441453a120bf","repo":"crewAIInc/crewAI","slug":"database-name-is-required-in-the-uri-260444","errorCode":null,"errorMessage":"Database name is required in the URI","messagePattern":"Database name is required in the URI","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/rag/loaders/postgres_loader.py","lineNumber":48,"sourceCode":"            raise ValueError(\"Database URI is required for PostgreSQL loader\")\n\n        query = source.source\n\n        parsed = urlparse(db_uri)\n        if parsed.scheme not in [\"postgresql\", \"postgres\", \"postgresql+psycopg2\"]:\n            raise ValueError(f\"Invalid PostgreSQL URI scheme: {parsed.scheme}\")\n\n        connection_params = {\n            \"host\": parsed.hostname or \"localhost\",\n            \"port\": parsed.port or 5432,\n            \"user\": parsed.username,\n            \"password\": parsed.password,\n            \"database\": parsed.path.lstrip(\"/\") if parsed.path else None,\n            \"cursor_factory\": RealDictCursor,\n        }\n\n        if not connection_params[\"database\"]:\n            raise ValueError(\"Database name is required in the URI\")\n\n        try:\n            connection = connect(**connection_params)\n            try:\n                with connection.cursor() as cursor:\n                    cursor.execute(query)\n                    rows = cursor.fetchall()\n\n                    if not rows:\n                        content = \"No data found in the table\"\n                        return LoaderResult(\n                            content=content,\n                            metadata={\"source\": query, \"row_count\": 0},\n                            doc_id=self.generate_doc_id(\n                                source_ref=query, content=content\n                            ),\n                        )\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/rag/loaders/postgres_loader.py#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Append the database name to the URI path: postgresql://user:pass@host:5432/mydb.","Verify the env var supplying the database name is set and non-empty before building the URI.","If the URI is built by concatenation, log the final string (with credentials masked) once to confirm it ends with /<dbname>.","Remember the loader only supports database selection via the URI — there is no separate database kwarg."],"exampleFix":"# before\ndb_uri = f\"postgresql://user:pass@host:5432{os.getenv('POSTGRES_DB', '')}\"  # empty -> error\n\n# after\ndb_name = os.environ[\"POSTGRES_DB\"]  # fail fast if unset\ndb_uri = f\"postgresql://user:pass@host:5432/{db_name}\"","handlingStrategy":"validation","validationCode":"from urllib.parse import urlparse\n\ndef has_database_name(uri: str) -> bool:\n    path = urlparse(uri).path\n    return bool(path.lstrip(\"/\"))","typeGuard":null,"tryCatchPattern":"try:\n    result = pg_loader.load(src, metadata={\"db_uri\": uri})\nexcept ValueError as e:\n    if \"Database name is required\" in str(e):\n        raise ConfigError(f\"URI missing /dbname: {mask(uri)}\") from e\n    raise","preventionTips":["Assemble URIs with a template that requires a non-empty db name: f'postgresql://.../{db}' where db is validated first.","Use os.environ['DB_NAME'] (KeyError) rather than .get() so an unset name fails loudly.","Unit-test your URI builder to catch empty database segments."],"tags":["postgres","database","uri","configuration"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}