Database Management
Database integration and management for ElizaOS
The @elizaos/plugin-sql
provides comprehensive database management for ElizaOS agents, featuring automatic schema migrations, multi-database support, and a sophisticated plugin architecture.
Key Features
🗄️ Dual Database Support
- PGLite - Embedded PostgreSQL for development
- PostgreSQL - Full PostgreSQL for production
- Automatic adapter selection based on environment
🔄 Dynamic Migration System
- Automatic schema discovery from plugins
- Intelligent table creation and updates
- Dependency resolution for foreign keys
- No manual migration files needed
🏗️ Schema Introspection
- Analyzes existing database structure
- Detects missing columns and indexes
- Handles composite primary keys
- Preserves existing data
🔌 Plugin Integration
- Plugins can define their own schemas
- Automatic table creation at startup
- Isolated namespaces prevent conflicts
- Shared core tables for common data
Architecture Overview
The plugin consists of several key components:
Core Components
1. Database Adapters
- BaseDrizzleAdapter - Shared functionality
- PgliteDatabaseAdapter - Development database
- PgDatabaseAdapter - Production database
2. Migration Service
- DatabaseMigrationService - Orchestrates migrations
- DrizzleSchemaIntrospector - Analyzes schemas
- Custom Migrator - Executes schema changes
3. Connection Management
- PGliteClientManager - Singleton PGLite instance
- PostgresConnectionManager - Connection pooling
- Global Singletons - Prevents multiple connections
4. Schema Definitions
Core tables for agent functionality:
agents
- Agent identitiesmemories
- Knowledge storageentities
- People and objectsrelationships
- Entity connectionsmessages
- Communication historyembeddings
- Vector searchcache
- Key-value storagelogs
- System events
Installation
Configuration
The SQL plugin is automatically included by the ElizaOS runtime and configured via environment variables.
Environment Setup
Create a .env
file in your project root:
Adapter Selection
The plugin automatically chooses the appropriate adapter:
- With
POSTGRES_URL
→ PostgreSQL adapter (production) - Without
POSTGRES_URL
→ PGLite adapter (development)
No code changes needed - just set your environment variables.
Custom Plugin with Schema
How It Works
1. Initialization
When the agent starts:
- SQL plugin initializes the appropriate database adapter
- Migration service discovers all plugin schemas
- Schemas are analyzed and dependencies resolved
- Tables are created or updated as needed
2. Schema Discovery
3. Dynamic Migration
The system:
- Introspects existing database structure
- Compares with plugin schema definitions
- Generates and executes necessary DDL
- Handles errors gracefully
4. Runtime Access
Plugins access the database through the runtime:
Advanced Features
Composite Primary Keys
Foreign Key Dependencies
Tables with foreign keys are automatically created in the correct order.
Schema Introspection
The system can analyze and adapt to existing database structures.
Error Recovery
- Automatic retries with exponential backoff
- Detailed error logging
- Graceful degradation
Best Practices
- Define Clear Schemas - Use TypeScript for type safety
- Use UUIDs - For distributed compatibility
- Include Timestamps - Track data changes
- Index Strategically - For query performance
- Test Migrations - Verify schema changes locally
Limitations
- No automatic downgrades or rollbacks
- Column type changes require manual intervention
- Data migrations must be handled separately
- Schema changes should be tested thoroughly
Next Steps
- Database Adapters - Detailed adapter documentation
- Schema Management - Creating and managing schemas
- Plugin Tables Guide - Adding tables to your plugin
- Examples - Real-world usage patterns