Skip to main content

PostgreSQL Storage

PostgresStorage stores key-value data in PostgreSQL using a kv_store table with JSONB values. It requires the pg package and is ideal for production deployments, especially when running multiple server instances.

Installation


Setup

1

Install pg

Add the pg package to your project.
2

Create storage instance

Pass a PostgreSQL connection string to the constructor.
3

Call initialize()

Creates the kv_store table if it doesn’t exist. Required before any get/set operations.

Constructor

string
required
PostgreSQL connection string. Format: postgresql://user:password@host:port/database

Full Example


Schema

PostgresStorage creates a table kv_store: Primary key: (namespace, key).

Multi-Instance Deployments

PostgresStorage is designed for horizontal scaling. Multiple Node.js processes can safely share the same PostgreSQL database.

Connection Pooling

The pg driver manages a connection pool internally. For high-concurrency deployments, configure the pool size:

Load Balancer Setup

All instances read and write to the same kv_store table. The (namespace, key) primary key ensures consistency, and JSONB values allow atomic updates.

Best Practices

  • Use a managed PostgreSQL service (RDS, Cloud SQL, Supabase) for production
  • Enable SSL for database connections: ?sslmode=require in the connection string
  • Monitor connection pool usage to avoid exhaustion under load
  • Run VACUUM ANALYZE kv_store periodically for optimal query performance

Environment Variables


API Reference