{"record":{"id":"1ac52c9a45c4700c","repo":"crewAIInc/crewAI","slug":"postgresql-database-error-e","errorCode":null,"errorMessage":"PostgreSQL database error: {e}","messagePattern":"PostgreSQL database error: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"lib/crewai-tools/src/crewai_tools/rag/loaders/postgres_loader.py","lineNumber":99,"sourceCode":"                    content = \"\\n\".join(text_parts)\n\n                    if len(content) > 100000:\n                        content = content[:100000] + \"\\n\\n[Content truncated...]\"\n\n                    return LoaderResult(\n                        content=content,\n                        metadata={\n                            \"source\": query,\n                            \"database\": connection_params[\"database\"],\n                            \"row_count\": len(rows),\n                            \"columns\": columns,\n                        },\n                        doc_id=self.generate_doc_id(source_ref=query, content=content),\n                    )\n            finally:\n                connection.close()\n        except Error as e:\n            raise ValueError(f\"PostgreSQL database error: {e}\") from e\n        except Exception as e:\n            raise ValueError(f\"Failed to load data from PostgreSQL: {e}\") from e\n","sourceCodeStart":81,"sourceCodeEnd":102,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/crewai-tools/src/crewai_tools/rag/loaders/postgres_loader.py#L81-L102","documentation":"Thrown by PGFileLoader when psycopg raises a database-level Error while executing the query or fetching rows: bad SQL syntax, missing tables/columns, permission denied on a relation, or a query cancelled by statement_timeout. It wraps psycopg3's Error hierarchy into ValueError, preserving the original as __cause__.","triggerScenarios":"The source string passed to load() is executed verbatim via cursor.execute(query). Typos in SQL, referencing tables that do not exist in the target database, insufficient privileges, or queries that exceed server-side timeouts all surface here. Because the query text is interpolated nowhere, SQL values with parameters (e.g. expecting %s binding) will also fail.","commonSituations":"Pointing the loader at production where table names differ from staging; using SELECT * on views the service role cannot read; long analytical queries hitting statement_timeout; copy-pasting interactive psql SQL that contains psql-only syntax (\\\\d, ; handled differently).","solutions":["Run the exact query manually with psql or a DB client using the same credentials to see the real error, then fix the SQL.","Confirm the table/view exists in the target database and the URI user has SELECT privilege on it.","For long queries, raise statement_timeout on the role or session, or pre-aggregate the data.","Catch ValueError and inspect e.__cause__ — it is the original psycopg Error with the server's SQLSTATE details."],"exampleFix":"# before\nresult = loader.load(SourceContent(source=\"SELECT * FROM user\"), metadata=md)  # typo: users\n\n# after\nresult = loader.load(SourceContent(source=\"SELECT id, name FROM users LIMIT 1000\"), metadata=md)","handlingStrategy":"validation","validationCode":"import psycopg\n\ndef query_is_valid(conn_string: str, query: str) -> bool:\n    with psycopg.connect(conn_string) as conn:\n        conn.execute(\"SELECT 1 FROM (\" + query.rstrip(';') + \") q LIMIT 0\")\n    return True  # raises on bad SQL/permissions","typeGuard":null,"tryCatchPattern":"try:\n    result = pg_loader.load(src, metadata=md)\nexcept ValueError as e:\n    cause = e.__cause__\n    if \"database error\" in str(e).lower():\n        log.error(\"SQL failed: %s\", getattr(cause, \"diag\", None) or cause)","preventionTips":["Validate SQL against the same database/role before handing it to the loader.","Prefer explicit column lists over SELECT * so schema drift fails your tests first.","Read e.__cause__ for the SQLSTATE diag instead of parsing the wrapped message."],"tags":["postgres","database","sql","rag"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}