PostgreSQL Requirements
Template Worker requires PostgreSQL 12 or later with the following features:- Support for JSONB data types
- Support for timestamps with time zones
- UUID extension support
Connection Configuration
The PostgreSQL connection URL is configured inconfig.yaml:
Connection URL Format
PostgreSQL username
PostgreSQL password
PostgreSQL server hostname or IP address
PostgreSQL server port
Database name
Connection Examples
Connection Pooling
Template Worker uses SQLx’s connection pooling with configurable pool sizes.Pool Configuration
Maximum number of connections in the poolSet via command-line argument. See Environment Variables for details.
Pool Behavior
- Connections are created lazily as needed
- Idle connections are maintained for reuse
- Failed connections trigger automatic reconnection
- Each worker process (in process pool mode) maintains its own connection pool
Pool Sizing Guidelines
Process Pool Mode (recommended for production):max_connections setting is sufficient:
Database Migrations
Template Worker includes an automatic migration system that tracks and applies schema changes.Migration System
Migrations are:- Stored in
src/migrations/directory - Applied automatically on first startup
- Tracked in the
_migrations_appliedtable - Applied in a specific order to maintain dependencies
Current Migrations
The following migrations are included:- khronosvalue_v2: Updates Khronos value storage format
- kv_generic: Creates generic key-value storage tables
- tenantstate: Adds tenant state management tables
- cleanup_v8: Removes legacy V8 runtime data
- drop_tenant_kv_expires_at: Removes deprecated expiry column
Applying Migrations
Automatic (on startup): Migrations are applied automatically when Template Worker starts with any worker type exceptregister.
Manual (using migrate command):
- Connect to the database
- Create the
_migrations_appliedtable if needed - Check which migrations have been applied
- Apply pending migrations in order
- Record successful migrations
- Exit
Migration Table Schema
The migration tracking table is automatically created:Migration Status
Check which migrations have been applied:Database Schema
Template Worker creates and manages several key tables:Tenant State
Stores Discord guild (tenant) configuration and state:Key-Value Storage
Generic key-value storage for template data:Migration Tracking
Tracks applied database migrations:Docker Setup
When using the provideddocker-compose.yml, PostgreSQL is configured automatically:
Database Persistence
Data is persisted in./deploy/docker/state/postgres/ on the host machine.
Health Checks
The database container includes health checks that Template Worker waits for:Database Operations
Backup
Restore
Reset Database
Monitoring and Troubleshooting
Connection Issues
If Template Worker fails to connect:-
Verify PostgreSQL is running:
-
Check connection URL:
-
Review logs:
-
Check firewall and network:
Migration Failures
If a migration fails:-
Check migration status:
- Review error logs from the migrate command
- Manual intervention may be required - migrations are not automatically rolled back
- Do not modify migration order - migrations have dependencies
Performance Monitoring
Production Best Practices
-
Connection Limits: Calculate total connections needed:
- Connection Pooling: Use external poolers like PgBouncer for very large deployments
-
SSL/TLS: Enable SSL for production databases:
- Monitoring: Set up PostgreSQL monitoring (pg_stat_statements, logging)
- Backups: Implement automated backup strategy with point-in-time recovery
- High Availability: Consider PostgreSQL replication or managed services
-
Resource Limits: Set appropriate
shared_buffers,work_mem, andmax_connectionsin PostgreSQL