fixes to postgres issue

This commit is contained in:
vasilije 2025-08-27 17:47:51 +02:00
parent 2413b7272b
commit 2e07c6cbc1
3 changed files with 33 additions and 10 deletions

View file

@ -39,8 +39,15 @@ def create_relational_engine(
connection_string = f"sqlite+aiosqlite:///{db_path}/{db_name}"
if db_provider == "postgres":
connection_string = (
f"postgresql+asyncpg://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}"
)
try:
# Test if asyncpg is available
import asyncpg
connection_string = (
f"postgresql+asyncpg://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}"
)
except ImportError:
raise ImportError(
"PostgreSQL dependencies are not installed. Please install with 'pip install cognee[postgres]' or 'pip install cognee[postgres-binary]' to use PostgreSQL functionality."
)
return SQLAlchemyAdapter(connection_string)

View file

@ -7,7 +7,16 @@ from sqlalchemy import JSON, Column, Table, select, delete, MetaData
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.exc import ProgrammingError
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential
from asyncpg import DeadlockDetectedError, DuplicateTableError, UniqueViolationError
try:
from asyncpg import DeadlockDetectedError, DuplicateTableError, UniqueViolationError
except ImportError:
# PostgreSQL dependencies not installed, define dummy exceptions
class DeadlockDetectedError(Exception):
pass
class DuplicateTableError(Exception):
pass
class UniqueViolationError(Exception):
pass
from cognee.shared.logging_utils import get_logger
@ -70,9 +79,13 @@ class PGVectorAdapter(SQLAlchemyAdapter, VectorDBInterface):
# Has to be imported at class level
# Functions reading tables from database need to know what a Vector column type is
from pgvector.sqlalchemy import Vector
self.Vector = Vector
try:
from pgvector.sqlalchemy import Vector
self.Vector = Vector
except ImportError:
raise ImportError(
"PostgreSQL dependencies are not installed. Please install with 'pip install cognee[postgres]' or 'pip install cognee[postgres-binary]' to use PGVector functionality."
)
async def embed_data(self, data: list[str]) -> list[list[float]]:
"""

View file

@ -72,15 +72,18 @@ distributed = [
"modal>=1.0.5,<2.0.0",
]
# Database backends
neo4j = ["neo4j>=5.28.0,<6"]
neptune = ["langchain_aws>=0.2.22"]
# PostgreSQL support (binary - no compilation required)
postgres = [
"psycopg2>=2.9.10,<3",
"psycopg2-binary>=2.9.10,<3.0.0", # Pre-compiled binary, no PostgreSQL headers needed
"pgvector>=0.3.5,<0.4",
"asyncpg>=0.30.0,<1.0.0",
]
postgres-binary = [
"psycopg2-binary>=2.9.10,<3.0.0",
# PostgreSQL support (source - requires PostgreSQL development headers)
postgres-source = [
"psycopg2>=2.9.10,<3 ; platform_system != 'Windows'", # Requires libpq-dev, build tools
"pgvector>=0.3.5,<0.4",
"asyncpg>=0.30.0,<1.0.0",
]