Phidata supports using PostgreSQL as a storage backend for Agents using the PgAgentStorage class.

Usage

Run PgVector

Install docker desktop and run PgVector on port 5532 using:

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16
storage.py
from phi.storage.agent.postgres import PgAgentStorage

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Create a storage backend using the Postgres database
storage = PgAgentStorage(
    # store sessions in the ai.sessions table
    table_name="agent_sessions",
    # db_url: Postgres database URL
    db_url=db_url,
)

# Add storage to the Agent
agent = Agent(storage=storage)

Params

ParameterTypeDefaultDescription
table_namestr-Name of the table to be used.
schemaOptional[str]"ai"Schema name, default is "ai".
db_urlOptional[str]NoneDatabase URL, if provided.
db_engineOptional[Engine]NoneDatabase engine to be used.
schema_versionint1Version of the schema, default is 1.
auto_upgrade_schemaboolFalseIf true, automatically upgrades the schema when necessary.