# @elizaos/plugin-sql SQL database adapter plugin for elizaOS — provides persistent storage via PostgreSQL or embedded PGlite (WASM), with Drizzle ORM, automatic schema migrations, and optional Row Level Security. ## Purpose / role This plugin registers a `DatabaseAdapter` with the elizaOS agent runtime so that all core runtime persistence (memories, entities, rooms, tasks, cache, logs, relationships, etc.) works against a real SQL backend. It is the default database plugin; elizaOS agents load it automatically if no other adapter is already registered. On Node/Bun it selects PostgreSQL when `POSTGRES_URL` is set, otherwise falls back to embedded PGlite. In the browser build it always uses PGlite (WASM). ## Plugin surface The exported `plugin` object (`src/index.ts` / `src/index.node.ts` / `src/index.browser.ts`) registers: | Kind | Name | Description | |------|------|-------------| | Service | `AdvancedMemoryStorageService` (`serviceType = "memoryStorage"`) | Implements `MemoryStorageProvider`; persists long-term memories and session summaries to dedicated SQL tables via the runtime memory API | | Schema | `schema` (all tables) | Passed as `plugin.schema` so `DatabaseMigrationService` can auto-migrate at startup | No actions, providers, evaluators, routes, or event handlers are registered by this plugin. ## Layout ``` plugins/plugin-sql/ package.json npm manifest; scripts, deps README.md human-facing docs src/ index.ts Default entry (same implementation as index.node.ts; uses ./utils) index.node.ts Node/Bun entry: PostgreSQL + PGlite; createDatabaseAdapter() index.browser.ts Browser entry: PGlite-only plugin base.ts BaseDrizzleAdapter — shared IDatabaseAdapter implementation types.ts DrizzleDatabase union type; getDb() helper agent-mapping.ts Utilities for normalizing agent message examples from DB rows utils.ts / utils.node.ts / utils.browser.ts Platform-specific helpers (resolvePgliteDir) utils/ string-to-uuid.ts String-to-UUID conversion utility connector-credential-store.ts ConnectorCredentialStore/Vault interfaces + factory migration-service.ts DatabaseMigrationService — discovers plugin schemas, runs migrations, re-applies RLS migrations.ts One-off migrations (e.g., entity RLS backfill) rls.ts Row Level Security helpers (install/apply/uninstall) pg/ adapter.ts PgDatabaseAdapter (wraps BaseDrizzleAdapter for Postgres) manager.ts PostgresConnectionManager — pg Pool singleton, withEntityContext sslmode.ts SSL mode resolver pglite/ adapter.ts PgliteDatabaseAdapter (wraps BaseDrizzleAdapter for PGlite) manager.ts PGliteClientManager — PGlite singleton, lifecycle errors.ts PGlite-specific error types neon/ adapter.ts NeonDatabaseAdapter — serverless adapter using @neondatabase/serverless manager.ts NeonConnectionManager — WebSocket-based connection for Neon/Vercel/Cloudflare schema/ index.ts Re-exports all table definitions agent.ts / room.ts / memory.ts / entity.ts / ... One file per table services/ advanced-memory-storage.ts AdvancedMemoryStorageService implementation stores/ agent.store.ts / memory.store.ts / room.store.ts / ... Query logic split by domain runtime-migrator/ index.ts RuntimeMigrator entry runtime-migrator.ts Diff-based migration engine schema-transformer.ts Drizzle schema → SQL diff extension-manager.ts PGlite extension loading write-back/ index.ts WriteBackService — forwards local PGlite writes to cloud API (Electric Pattern 1) drizzle/ Drizzle ORM re-exports ``` ## Commands All scripts run from the plugin root via `bun run --cwd plugins/plugin-sql