diff --git a/cognee/infrastructure/databases/relational/create_relational_engine.py b/cognee/infrastructure/databases/relational/create_relational_engine.py index b2a79c818..e3a9d3506 100644 --- a/cognee/infrastructure/databases/relational/create_relational_engine.py +++ b/cognee/infrastructure/databases/relational/create_relational_engine.py @@ -1,3 +1,4 @@ +from sqlalchemy import URL from .sqlalchemy.SqlAlchemyAdapter import SQLAlchemyAdapter from functools import lru_cache @@ -45,9 +46,16 @@ def create_relational_engine( # Test if asyncpg is available import asyncpg - connection_string = ( - f"postgresql+asyncpg://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}" + # Handle special characters in username and password like # or @ + connection_string = URL.create( + "postgresql+asyncpg", + username=db_username, + password=db_password, + host=db_host, + port=int(db_port), + database=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." diff --git a/cognee/infrastructure/databases/vector/create_vector_engine.py b/cognee/infrastructure/databases/vector/create_vector_engine.py index 02e01e288..8a87f0339 100644 --- a/cognee/infrastructure/databases/vector/create_vector_engine.py +++ b/cognee/infrastructure/databases/vector/create_vector_engine.py @@ -1,3 +1,5 @@ +from sqlalchemy import URL + from .supported_databases import supported_databases from .embeddings import get_embedding_engine from cognee.infrastructure.databases.graph.config import get_graph_context_config @@ -66,8 +68,13 @@ def create_vector_engine( if not (db_host and db_port and db_name and db_username and db_password): raise EnvironmentError("Missing requred pgvector credentials!") - connection_string: str = ( - f"postgresql+asyncpg://{db_username}:{db_password}@{db_host}:{db_port}/{db_name}" + connection_string = URL.create( + "postgresql+asyncpg", + username=db_username, + password=db_password, + host=db_host, + port=int(db_port), + database=db_name, ) try: