Skip to content
Changelog

Create PostgreSQL engines with Agno's defaults

September 8, 20261 min read

Agno's create_postgres_engine and create_async_postgres_engine live in agno.db.postgres and take only the pool settings you want to change. You used to copy Agno's defaults into your own engine to tune the pool.

from agno.db.postgres import PostgresDb, create_postgres_engine
 
engine = create_postgres_engine(
    "postgresql://ai:ai@localhost:5532/ai",
    pool_size=5,
    connect_args={"connect_timeout": 5},
)
db = PostgresDb(db_engine=engine)

The factories default to pool_pre_ping=True, pool_recycle=3600 and Agno's JSON serializer, and any keyword you pass overrides them. A plain postgres:// or postgresql:// URL selects Psycopg 3. An explicit driver and its TLS settings stay as written. Neither factory opens a connection until something uses the engine.

create_postgres_engine builds one engine with pool_pre_ping, pool_recycle, Agno's JSON serializer and your settings on top. PostgresDb takes it as db_engine, and PgVector and DbFileSystem borrow it through db, so sessions, vectors and files share the same connections.

Pass the engine to PostgresDb(db_engine=...), and PgVector(db=db) and DbFileSystem(db=db) can borrow the same pool. PostgresDb(db_url=...) picks its driver the same way it did before.

See the cookbook, and learn more about PostgresDb in the documentation.

Frequently asked questions

Build an engine with create_postgres_engine and pass it to PostgresDb(db_engine=engine). PgVector(db=db) and DbFileSystem(db=db) then borrow the same pool.

create_postgres_engine sets pool_pre_ping=True, pool_recycle=3600 and Agno's JSON serializer. Any keyword you pass overrides those defaults.

Agno picks Psycopg 3 for a plain postgres:// or postgresql:// URL. If your URL names a driver, Agno keeps that driver and its TLS settings as written.

Shipped around the same time