CockroachDB
CockroachDB is a distributed SQL database that uses the PostgreSQL wire protocol. It works with Memori through psycopg2 (Python) or the pg driver (TypeScript) — no special adapter needed.
Install
Install
pip install memori psycopg2-binary
Quick Start
CockroachDB Connection
from memori import Memori
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine(
"cockroachdb+psycopg2://user:password@localhost:26257/memori_db",
pool_pre_ping=True
)
SessionLocal = sessionmaker(bind=engine)
mem = Memori(conn=SessionLocal)
mem.config.storage.build()
Connection Strings
| Environment | Connection String |
|---|---|
| Local | cockroachdb+psycopg2://root@localhost:26257/memori_db |
| With Auth | cockroachdb+psycopg2://user:pass@host:26257/memori_db |
| CockroachDB Cloud | cockroachdb+psycopg2://user:pass@cluster.cockroachlabs.cloud:26257/memori_db?sslmode=verify-full |
| PostgreSQL scheme | postgresql+psycopg2://user:pass@host:26257/memori_db |
For TypeScript, set COCKROACHDB_CONNECTION_STRING in your .env file and append ?sslmode=verify-full for CockroachDB Cloud.
Complete Example
Complete Example
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from memori import Memori
from openai import OpenAI
engine = create_engine(
os.getenv("COCKROACHDB_URL"),
pool_pre_ping=True,
pool_size=10,
max_overflow=20,
pool_recycle=300
)
SessionLocal = sessionmaker(bind=engine)
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
mem = Memori(conn=SessionLocal).llm.register(client)
mem.attribution(entity_id="user_123", process_id="my_agent")
mem.config.storage.build()
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "I manage a distributed systems team."}]
)
print(response.choices[0].message.content)
mem.augmentation.wait()
facts = mem.recall("job role")
print(facts)
Notes (TypeScript)
- Pass a factory function:
conn: () => pool. Memori never closes the pool — you own its lifecycle and callpool.end()when you're done. - Use a
pg.Pool, not apg.Client— pooling is required for CockroachDB's connection model. - Set
COCKROACHDB_CONNECTION_STRINGin your.envfile. - For CockroachDB Cloud, append
?sslmode=verify-fullto your connection string.