Skip to main content
PostgresStorage implements core’s StorageDriver using a PostgreSQL kv_store table. Use it for supported conversation, memory, or configuration records when you already operate PostgreSQL. It is a key/value driver, not the lease-based store for durable tasks or the pgvector search adapter.

Installation

Use an ESM TypeScript project ("type": "module" in package.json) on a supported Node version.
Supply a reachable database and a role allowed to create/use kv_store. These programs connect to that database when run; the documentation type check does not create tables.

Setup

Set DATABASE_URL through your host environment. Save as storage.ts:
Expect { value: 42 }. The example removes its one demo record; the initialized table remains. A missing-key read returns null. Resolve authentication, network, or table-permission errors before connecting an Agent.

Constructor

The constructor takes one connection-string argument:
Call initialize() before operations. The host owns the returned driver and calls close() after all consumers finish.

Full example

Add the openai optional dependency and set OPENAI_API_KEY for this live-model example. Use a session/user identity supplied by your trusted application boundary when adapting it to a server.
The Agent closes its owned services; the host closes the shared driver exactly once. Continue with sessions for identity and history behavior. Admin blueprints and Agent history are different records even when they use the same database.

Schema

initialize() creates the table if absent: The primary key is (namespace, key). set() uses an upsert to replace one entire value. Namespaces organize records; they are not tenant authorization rules.

Multi-instance deployments

Processes can connect to the same PostgreSQL table, but that alone does not coordinate a multi-step read → modify → write sequence or concurrent session turns. Two writers can overwrite the same record. Use application coordination appropriate to the operation and an atomic durable-store contract when task claims/leases are required.

Connection pooling

The adapter constructs an internal pg.Pool from the connection string. V4 has no second constructor argument for pool options and no injected-pool overload. Do not pass { max: 20 } to PostgresStorage. Budget connections across processes and any external database pooler. If you require pool controls outside this adapter’s contract, implement a StorageDriver with your host-owned database client rather than reaching into private adapter fields.

Load balancer setup

A load balancer distributes requests; it does not serialize turns sharing a session. Pair your routing with trusted resource ownership and a strategy for concurrent updates. Closing one instance’s pool does not close pools in other instances.

Best practices

Keep credentials in host configuration, use the database’s verified TLS settings, initialize before admitting work, and monitor errors/connection usage. Plan retention, backups, and schema access for the data you store. Database persistence is not automatic interrupted-run recovery.

Environment variables

DATABASE_URL is a convention used by these examples; the constructor receives a string and does not read that variable itself.

API reference

See storage APIs for exact declarations and recovery for choosing the right persistence layer.