PostgreSQL Storage
PostgresStorage stores key-value data in PostgreSQL using akv_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/databaseFull Example
Schema
PostgresStorage creates a tablekv_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
Thepg driver manages a connection pool internally. For high-concurrency deployments, configure the pool size:
Load Balancer Setup
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=requirein the connection string - Monitor connection pool usage to avoid exhaustion under load
- Run
VACUUM ANALYZE kv_storeperiodically for optimal query performance