From f48df27fc85b0df53701f0bf813563dc20494f74 Mon Sep 17 00:00:00 2001 From: hiyan Date: Thu, 11 Dec 2025 10:32:45 +0530 Subject: [PATCH 1/2] fix(db): url-encode postgres credentials to handle special characters --- .../databases/relational/create_relational_engine.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cognee/infrastructure/databases/relational/create_relational_engine.py b/cognee/infrastructure/databases/relational/create_relational_engine.py index deaeaa2da..8813dfcb2 100644 --- a/cognee/infrastructure/databases/relational/create_relational_engine.py +++ b/cognee/infrastructure/databases/relational/create_relational_engine.py @@ -1,5 +1,6 @@ from .sqlalchemy.SqlAlchemyAdapter import SQLAlchemyAdapter from functools import lru_cache +from urllib.parse import quote_plus @lru_cache @@ -43,9 +44,10 @@ 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}" - ) + encoded_username = quote_plus(db_username) + encoded_password = quote_plus(db_password) + + connection_string = f"postgresql+asyncpg://{encoded_username}:{encoded_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." From 6e5e79f434a755b0692bb6956b571128e9d9db4c Mon Sep 17 00:00:00 2001 From: Igor Ilic Date: Wed, 17 Dec 2025 21:07:23 +0100 Subject: [PATCH 2/2] fix: Resolve connection issue with postgres when special characters are present --- .../relational/create_relational_engine.py | 14 ++++++++++---- .../databases/vector/create_vector_engine.py | 11 +++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cognee/infrastructure/databases/relational/create_relational_engine.py b/cognee/infrastructure/databases/relational/create_relational_engine.py index 8813dfcb2..ea2b35c75 100644 --- a/cognee/infrastructure/databases/relational/create_relational_engine.py +++ b/cognee/infrastructure/databases/relational/create_relational_engine.py @@ -1,6 +1,6 @@ +from sqlalchemy import URL from .sqlalchemy.SqlAlchemyAdapter import SQLAlchemyAdapter from functools import lru_cache -from urllib.parse import quote_plus @lru_cache @@ -44,10 +44,16 @@ def create_relational_engine( # Test if asyncpg is available import asyncpg - encoded_username = quote_plus(db_username) - encoded_password = quote_plus(db_password) + # 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, + ) - connection_string = f"postgresql+asyncpg://{encoded_username}:{encoded_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." diff --git a/cognee/infrastructure/databases/vector/create_vector_engine.py b/cognee/infrastructure/databases/vector/create_vector_engine.py index d1cf855d7..47a2e2582 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 @@ -61,8 +63,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: