Back to Agno

02 Databases

cookbook/05_agent_os/02_databases/README.md

2.8.24.1 KB
Original Source

02 Databases

Pass a database to AgentOS(db=...) to make it the default for agents, teams, and workflows that do not provide their own database. Use SQLite for local development and Postgres for production; the reference table below covers other supported storage adapters without repeating the same AgentOS example.

Files

FileDescription
basic.pyDemonstrates default-database inheritance and automatic table provisioning with SQLite.
postgres.pySelects a synchronous or asynchronous Postgres adapter for production persistence.
surreal.pyShows SurrealDB's client, credentials, namespace, and database constructor shape.

Default database and provisioning

AgentOS assigns its database to each listed agent, team, and workflow whose own db is unset. A component-level database always takes precedence. auto_provision_dbs=True is the default and creates the required tables during server startup; disable it only when an external migration process owns the schema.

Backend reference

BackendImportConnectionRequired service
SQLitefrom agno.db.sqlite import SqliteDbSqliteDb(db_file="tmp/agent_os.db")None
JSONfrom agno.db.json import JsonDbJsonDb(db_path="tmp/agent_os_json")None
Postgresfrom agno.db.postgres import PostgresDbPostgresDb(db_url="postgresql+psycopg://user:pass@host:5432/db")PostgreSQL
MySQLfrom agno.db.mysql import MySQLDbMySQLDb(db_url="mysql+pymysql://user:pass@host:3306/db")MySQL
MongoDBfrom agno.db.mongo import MongoDbMongoDb(db_url="mongodb://localhost:27017", db_name="agno")MongoDB
Redisfrom agno.db.redis import RedisDbRedisDb(db_url="redis://localhost:6379/0")Redis
Valkeyfrom agno.db.valkey import ValkeyDbValkeyDb(host="localhost", port=6379)Valkey
DynamoDBfrom agno.db.dynamo import DynamoDbDynamoDb()AWS DynamoDB and AWS_REGION, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
Firestorefrom agno.db.firestore import FirestoreDbFirestoreDb(project_id="my-project")Firestore and Application Default Credentials
GCS JSONfrom agno.db.gcs_json import GcsJsonDbGcsJsonDb(bucket_name="my-bucket")GCS and Application Default Credentials
SingleStorefrom agno.db.singlestore import SingleStoreDbSingleStoreDb(db_url="mysql+pymysql://user:pass@host:3306/db")SingleStore
SurrealDBfrom agno.db.surrealdb import SurrealDbSurrealDb(client=None, db_url=..., db_creds=..., db_ns=..., db_db=...)SurrealDB
ClickHousefrom agno.db.clickhouse import ClickhouseDbClickhouseDb(host="localhost", database="agno")Traces only; not a general AgentOS persistence backend
In-memoryfrom agno.db.in_memory import InMemoryDbInMemoryDb()None; process-local and non-durable

Neon and Supabase use the Postgres wire protocol, so pass their connection strings to PostgresDb rather than using a separate adapter. For asynchronous Postgres, import AsyncPostgresDb and use a postgresql+psycopg_async://... URL.

ClickHouse implements the trace and span surface. Use a row store such as Postgres for sessions, memories, knowledge, evals, and components.

Prerequisites

  • All examples need OPENAI_API_KEY only when an agent run calls the model.
  • Start Postgres with ./cookbook/scripts/run_pgvector.sh.
  • Install agno[surrealdb] and start SurrealDB with ./cookbook/scripts/run_surrealdb.sh.

Run

SQLite:

bash
.venvs/demo/bin/python cookbook/05_agent_os/02_databases/basic.py

Synchronous Postgres:

bash
.venvs/demo/bin/python cookbook/05_agent_os/02_databases/postgres.py

Asynchronous Postgres:

bash
AGENTOS_USE_ASYNC_POSTGRES=true \
  .venvs/demo/bin/python cookbook/05_agent_os/02_databases/postgres.py

SurrealDB:

bash
.venvs/demo/bin/python cookbook/05_agent_os/02_databases/surreal.py

Each server listens on port 7777. Read its database ID from GET /config, then run a schema migration with:

bash
curl -X POST http://localhost:7777/databases/<db-id>/migrate

To migrate to a specific schema version, add ?target_version=<version> to the URL.