Database Connectivity for AI Agents: SQL Server, Redis, and PostgreSQL
When building a production-ready AI agent, model intelligence alone isn’t enough. Behind every reliable agent is a well-designed data layer that handles accuracy, speed, and persistence. In this article, I’ll walk through a practical multi-database approach using SQL Server, Redis, and PostgreSQL — each serving a distinct and critical role.
We’ll cover how these systems work together to support schema awareness, session management, and long-term memory in an AI application.
SQL Server: The Single Source of Truth
SQL Server acts as the primary system of record, holding structured business data. To connect securely and efficiently, we use SQLAlchemy with the pyodbc driver.
One important optimization is schema caching. Instead of repeatedly querying metadata — which can be expensive and slow — we fetch the schema once and reuse it. This is especially useful for AI agents that dynamically generate SQL queries.
from sqlalchemy import create_engine
import pandas as pd
def get_db_engine():
server = "your_server"
database = "your_db"
username = "your_user"
password = "your_password"
connection_str = (
f"mssql+pyodbc://{username}:{password}@{server}:1433/{database}"
"?driver=ODBC+Driver+18+for+SQL+Server&Encrypt=yes&TrustServerCertificate=yes"
)
return create_engine(connection_str)
def fetch_schema(engine):
query = "SELECT table_name, column_name, data_type FROM information_schema.columns"
return pd.read_sql(query, engine)
With this setup, the agent gains a clear understanding of the database structure without placing unnecessary load on the server.
Redis: High-Performance Caching and Session State
While SQL Server is reliable, it’s not designed for ultra-fast lookups. That’s where Redis comes in.
Redis is used to cache:
Database schemas
User session state
Frequently accessed metadata
Download the Medium app
This allows the AI agent to retrieve critical information in milliseconds, significantly improving response time and scalability.
import redis
import json
def get_redis_client():
return redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
def cache_data(key, data, ttl=3600):
r = get_redis_client()
r.set(key, json.dumps(data), ex=ttl)
def get_cached_data(key):
r = get_redis_client()
data = r.get(key)
return json.loads(data) if data else None
Using a TTL (time-to-live) ensures cached data stays fresh while still delivering high performance.
PostgreSQL: Persistent Memory for Conversations
To give an AI agent continuity, it needs memory beyond a single session. For this, we use PostgreSQL as a persistent conversation store.
PostgreSQL is ideal here because it’s:
Durable
ACID-compliant
Excellent for auditing and historical analysis
Each user message and agent response is stored with timestamps and thread identifiers.
from sqlalchemy import create_engine, text
def init_memory_store(db_url):
engine = create_engine(db_url)
with engine.begin() as conn:
conn.execute(text("""
CREATE TABLE IF NOT EXISTS conversation_history (
id SERIAL PRIMARY KEY,
thread_id VARCHAR(64),
role VARCHAR(20),
content TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
)
"""))
return engine
def record_message(engine, thread_id, role, content):
with engine.begin() as conn:
conn.execute(
text("INSERT INTO conversation_history (thread_id, role, content) VALUES (:t, :r, :c)"),
{"t": thread_id, "r": role, "c": content}
)
This enables features like conversation recall, context-aware responses, and long-term personalization.
Why a Multi-Store Architecture Works
Each database is optimized for a specific responsibility:
SQL Server → Structured, authoritative business data
Redis → Ultra-fast access to cached schemas and session data
PostgreSQL → Durable, long-term conversational memory
By separating concerns, we avoid overloading a single system and gain the flexibility to scale each component independently.
Final Thoughts
A well-designed AI system is as much about infrastructure as it is about models. By combining SQL Server, Redis, and PostgreSQL, you get a data layer that’s fast, reliable, and production-ready.
This architecture allows AI agents to reason better, respond faster, and remember more — key ingredients for building real-world intelligent applications.