Database Adapters
Understanding PGLite and PostgreSQL adapters in the SQL plugin
The SQL plugin provides two database adapters that extend a common BaseDrizzleAdapter
:
- PGLite Adapter - Embedded PostgreSQL for development and testing
- PostgreSQL Adapter - Full PostgreSQL for production environments
Architecture Overview
Both adapters share the same base functionality through BaseDrizzleAdapter
, which implements the IDatabaseAdapter
interface from @elizaos/core
. The adapters handle:
- Connection management through dedicated managers
- Automatic retry logic for database operations
- Schema introspection and dynamic migrations
- Embedding dimension configuration (default: 384 dimensions)
PGLite Adapter
The PgliteDatabaseAdapter
uses an embedded PostgreSQL instance that runs entirely in Node.js.
Key Features
- Zero external dependencies - No PostgreSQL installation required
- File-based persistence - Data stored in local filesystem
- Singleton connection manager - Ensures single database instance per process
- Automatic initialization - Database created on first use
Implementation Details
Connection Management
The PGliteClientManager
handles:
- Singleton PGLite instance creation
- Data directory resolution and creation
- Connection persistence across adapter instances
PostgreSQL Adapter
The PgDatabaseAdapter
connects to a full PostgreSQL database using connection pooling.
Key Features
- Connection pooling - Efficient resource management
- Automatic retry logic - Built-in resilience for transient failures
- Production-ready - Designed for scalable deployments
- SSL support - Secure connections when configured
- Cloud compatibility - Works with Supabase, Neon, and other PostgreSQL providers
Implementation Details
Connection Management
The PostgresConnectionManager
provides:
- Connection pool management (default size: 20)
- SSL configuration based on environment
- Singleton pattern to prevent multiple pools
- Graceful shutdown handling
Adapter Selection
The adapter is automatically selected based on environment configuration:
Migration Handling
Important: Both adapters delegate migration handling to the DatabaseMigrationService
. The adapters themselves do not run migrations directly.
The migration service handles:
- Plugin schema discovery and registration
- Dynamic table creation and updates
- Schema introspection for existing tables
- Dependency resolution for table creation order
Best Practices
Development (PGLite)
- Use default data directory for consistency
- Clear data directory between test runs if needed
- Be aware of file system limitations
- Suitable for single-instance development
Production (PostgreSQL)
- Always use connection pooling
- Configure SSL for secure connections
- Monitor connection pool usage
- Use environment variables for configuration
- Implement proper backup strategies
Configuration
The SQL plugin automatically selects the appropriate adapter based on environment variables.
Environment Variables
Configuration Priority
- If
POSTGRES_URL
is set → Uses PostgreSQL adapter - If
POSTGRES_URL
is not set → Uses PGLite adapter- With
PGLITE_DATA_DIR
if specified - Otherwise uses default path:
./.eliza/.elizadb
- With
PostgreSQL Configuration
The PostgreSQL adapter supports any PostgreSQL-compatible database:
- Supabase - Use the connection string from your project settings
- Neon - Use the connection string from your Neon console
- Amazon RDS PostgreSQL
- Google Cloud SQL PostgreSQL
- Self-hosted PostgreSQL (v12+)
Example connection strings:
Error Handling
Both adapters include:
- Automatic retry logic (3 attempts by default)
- Exponential backoff between retries
- Detailed error logging
- Graceful degradation
The adapters handle common scenarios like:
- Connection timeouts
- Transient network failures
- Pool exhaustion (PostgreSQL)
- File system errors (PGLite)