PostgreSQL Adapter
The PostgreSQL adapter stores memories in a PostgreSQL database using the pg driver with connection pooling.
Installation
Section titled “Installation”npm install @youcraft/recall-adapter-postgresqlimport { createMemory } from '@youcraft/recall'import { postgresqlAdapter } from '@youcraft/recall-adapter-postgresql'
const db = postgresqlAdapter({ connection: process.env.DATABASE_URL!,})
const memory = createMemory({ db, embeddings, extractor })Configuration
Section titled “Configuration”| Option | Type | Default | Description |
|---|---|---|---|
connection | string | PoolConfig | required | Connection string or pg PoolConfig object |
tableName | string | "memories" | Name of the table to store memories |
Examples
Section titled “Examples”Connection string
Section titled “Connection string”const db = postgresqlAdapter({ connection: 'postgresql://user:password@localhost:5432/mydb',})Pool configuration
Section titled “Pool configuration”const db = postgresqlAdapter({ connection: { host: 'localhost', port: 5432, database: 'mydb', user: 'user', password: 'password', max: 20, // Maximum pool size },})Custom table name
Section titled “Custom table name”const db = postgresqlAdapter({ connection: process.env.DATABASE_URL!, tableName: 'user_memories',})Schema
Section titled “Schema”The adapter automatically creates this table structure:
CREATE TABLE IF NOT EXISTS memories ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL, content TEXT NOT NULL, embedding JSONB NOT NULL, metadata JSONB NOT NULL DEFAULT '{}', created_at TIMESTAMPTZ NOT NULL, updated_at TIMESTAMPTZ NOT NULL);CREATE INDEX IF NOT EXISTS idx_memories_user_id ON memories(user_id);Performance Considerations
Section titled “Performance Considerations”- Connection pooling: The adapter uses
pg.Poolfor efficient connection management - Vector search: Currently uses JavaScript-based cosine similarity
- For native vector search: Consider using pgvector extension for better performance at scale
- Indexing: User ID index is created automatically for efficient filtering