Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ DB_NAME=cognee_db
#DB_USERNAME=cognee
#DB_PASSWORD=cognee

# -- Advanced: Custom database connection arguments (optional) ---------------
# Pass additional connection parameters as JSON. Useful for SSL, timeouts, etc.
# Examples:
# For PostgreSQL with SSL:
# DATABASE_CONNECT_ARGS='{"sslmode": "require", "connect_timeout": 10}'
# For SQLite with custom timeout:
# DATABASE_CONNECT_ARGS='{"timeout": 60}'
#DATABASE_CONNECT_ARGS='{}'

################################################################################
# 🕸️ Graph Database settings
################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from os import path
import tempfile
from uuid import UUID
import json
from typing import Optional
from typing import AsyncGenerator, List
from contextlib import asynccontextmanager
Expand Down Expand Up @@ -30,9 +31,46 @@ class SQLAlchemyAdapter:
"""

def __init__(self, connection_string: str):
"""
Initialize the SQLAlchemy adapter with connection settings.

Parameters:
-----------
connection_string (str): The database connection string (e.g., 'sqlite:///path/to/db'
or 'postgresql://user:pass@host:port/db').

Environment Variables:
----------------------
DATABASE_CONNECT_ARGS: Optional JSON string containing connection arguments.
Allows configuration of driver-specific parameters such as SSL settings,
timeouts, or connection pool options without code changes.

Examples:
PostgreSQL with SSL:
DATABASE_CONNECT_ARGS='{"sslmode": "require", "connect_timeout": 10}'

SQLite with custom timeout:
DATABASE_CONNECT_ARGS='{"timeout": 60}'

Note: This follows cognee's environment-based configuration pattern and is
the recommended approach for production deployments.
"""
self.db_path: str = None
self.db_uri: str = connection_string

# Parse optional connection arguments from environment variable
connect_args = None
env_connect_args = os.getenv("DATABASE_CONNECT_ARGS")
if env_connect_args:
try:
connect_args = json.loads(env_connect_args)
if not isinstance(connect_args, dict):
logger.warning("DATABASE_CONNECT_ARGS is not a valid JSON dictionary, ignoring")
connect_args = None
except json.JSONDecodeError:
logger.warning("Failed to parse DATABASE_CONNECT_ARGS as JSON, ignoring")
connect_args = None

if "sqlite" in connection_string:
[prefix, db_path] = connection_string.split("///")
self.db_path = db_path
Expand All @@ -53,7 +91,7 @@ def __init__(self, connection_string: str):
self.engine = create_async_engine(
connection_string,
poolclass=NullPool,
connect_args={"timeout": 30},
connect_args={**{"timeout": 30}, **(connect_args or {})},
)
else:
self.engine = create_async_engine(
Expand All @@ -63,6 +101,7 @@ def __init__(self, connection_string: str):
pool_recycle=280,
pool_pre_ping=True,
pool_timeout=280,
connect_args=connect_args or {},
)

self.sessionmaker = async_sessionmaker(bind=self.engine, expire_on_commit=False)
Expand Down