Overview
DeployStack uses PostgreSQL as its database backend, providing enterprise-grade reliability, ACID compliance, and excellent performance. The system leverages Drizzle ORM for type-safe database operations with a modern, developer-friendly experience. PostgreSQL provides:- ACID Compliance - Full transactional support with rollback capabilities
- Connection Pooling - Efficient connection management via node-postgres
- Native Type System - Boolean, timestamp with timezone, JSONB, arrays, and more
- Horizontal Scaling - Read replicas and partitioning for production deployments
Database Setup and Configuration
The backend uses an environment-based configuration system where database credentials are provided via environment variables, and the database is initialized through the setup API.Setup Instructions: For step-by-step setup instructions, see the Database Setup Guide.
PostgreSQL Technical Guide: For detailed technical information, see the PostgreSQL Development Guide.
Environment Variables
Configure PostgreSQL by setting these environment variables:Database Status
Check the current status of the database configuration and initialization:- Endpoint:
GET /api/db/status - Method:
GET - Response: JSON object with database status information
Initial Database Setup
The initial database setup is performed through the frontend setup wizard at/setup, which provides a user-friendly interface. The wizard internally calls the backend API:
- Frontend URL:
https://<your-frontend-url>/setup - Backend Endpoint (called by frontend):
POST /api/db/setup - Method:
POST - Request Body: JSON object specifying the database type
Setup Request
API Response
The setup endpoint returns a JSON response indicating success and restart requirements: Successful Setup:Database Selection File
The database configuration is stored in:services/backend/persistent_data/db.selection.json(relative to the backend service directory)
https://<your-frontend-url>/setup. Manual editing is not recommended.
Example content:
/api/db/setup endpoint.
Architecture
Key Components
- Drizzle ORM: Type-safe ORM with native driver support
- node-postgres (pg): Native PostgreSQL driver with connection pooling
- Environment Configuration: Database credentials via environment variables
- Automatic Migrations: Migrations applied on server startup
Database Driver
The system uses the native PostgreSQL driver for optimal performance:Database Connection Patterns
When accessing the database in route handlers, always usegetDb() to obtain the database connection:
server.dbmay benullduring certain initialization statesgetDb()always returns the active database connection- This ensures consistent behavior across all endpoints
server.db as it can cause “Cannot read properties of null” errors.
Database Operations
Working with Query Results
PostgreSQL operations (INSERT, UPDATE, DELETE) return a result object withrowCount indicating the number of affected rows:
Standard Patterns
Delete Operations:Database Structure
DeployStack uses PostgreSQL-native types and features:Schema Files
src/db/schema.ts - PostgreSQL schema definition
- Native PostgreSQL types (
boolean,timestamp with timezone,jsonb) - Proper foreign key relationships and constraints
- Migration directory:
drizzle/migrations/
- Core application tables (users, teams, MCP configurations, etc.)
- Background job queue tables (
queueJobsandqueueJobBatches) - see Background Job Queue - Plugin table definitions (populated dynamically)
- Proper foreign key relationships and constraints
Making Schema Changes
Follow these steps to add or modify database tables:-
Modify Schema Definitions
Edit
src/db/schema-tables/[table-group].ts: -
Generate Migration
Run the migration generation command:
This generates SQL migration files in
drizzle/migrations/. -
Review Migrations
Examine the generated SQL files in
drizzle/migrations/to ensure they match your intended changes. -
Apply Migrations
Migrations are automatically applied on server startup. You can also run them manually:
-
Use the New Schema
Update your application code to use the new tables:
Migration Management
- Automatic Tracking: Migrations tracked in
__drizzle_migrationstable - Incremental Application: Only new migrations are applied
- Transaction Safety: Migrations applied in transactions for consistency
- Execution Timing: Migrations are applied automatically on server startup, but only after the database has been initialized through the setup process
/setup) must be completed first to create the database, then migrations will apply on subsequent server startups.
Global Settings Integration
During database setup, DeployStack automatically initializes global settings that configure the application:Automatic Initialization
The global settings system:- Loads setting definitions from all modules in
src/global-settings/ - Creates setting groups for organizing configuration options
- Initializes default values for all settings with proper encryption
- Uses efficient batch operations with PostgreSQL connection pooling
Global Settings Documentation: For detailed information about global settings, see the Global Settings Guide.
Plugin Database Extensions
Plugins can extend the database with their own tables and settings. For detailed information about plugin database integration, including table definitions, dynamic table creation, and security boundaries, see the Plugin System Guide. Key plugin database features:- Dynamic table creation at runtime (separate from core migrations)
- Automatic table prefixing with plugin IDs
- Security isolation between core and plugin tables
- Global settings integration for plugin configuration
Development Workflow
- Environment Setup: Configure PostgreSQL environment variables
- Initial Setup: Complete the frontend setup wizard at
/setup(for first-time setup)- This creates
persistent_data/db.selection.json - Initializes the PostgreSQL database
- For development, you can also directly call
POST /api/db/setup
- This creates
- Schema Changes: Modify
src/db/schema-tables/directory - Generate Migrations: Run
npm run db:generate - Apply Changes: Restart server or run
npm run db:up - Update Code: Use the modified schema in your application
PostgreSQL-Specific Features
Connection Pooling
- Efficient connection management via
node-postgres - Configurable pool size and timeout settings
- Automatic connection recycling
Native Types
- Boolean columns with native
booleantype - Timestamps with timezone support
- JSONB for efficient JSON storage
- Arrays and custom types
Advanced Features
- Multi-version concurrency control (MVCC)
- Point-in-time recovery and continuous archiving
- Full-text search capabilities
- Horizontal scaling with read replicas

