Elysia + PostgreSQL
Bun-first web framework with end-to-end type safety.
Overview
Elysia is a modern TypeScript web framework built specifically for Bun runtime, focusing on end-to-end type safety and exceptional performance. Created as a Bun-first alternative to Express and Fastify, Elysia leverages Bun's native APIs and optimizations to achieve significantly faster request handling while maintaining full TypeScript support throughout the entire application stack. The framework emphasizes developer experience with features like automatic OpenAPI generation, built-in validation, and the Eden client for type-safe API consumption.
This stack combines Elysia's high-performance web server capabilities with PostgreSQL's robust relational database features, creating a powerful foundation for building scalable web applications. Elysia's integration with PostgreSQL through ORMs like Drizzle enables compile-time type checking of database queries, ensuring that schema changes are caught during development rather than runtime. The combination leverages PostgreSQL's advanced features like JSON/JSONB support, full-text search, and ACID compliance while maintaining the performance benefits of Bun's optimized runtime.
Developers building modern web applications who prioritize both performance and type safety will find this stack particularly valuable. Teams transitioning from Node.js-based frameworks can benefit from Bun's faster startup times and reduced memory usage, while PostgreSQL's mature ecosystem and reliability make it ideal for applications requiring complex data relationships, transactions, or compliance requirements. This combination is especially suited for API-heavy applications, real-time services, and projects where end-to-end type safety is crucial.
Key Features
- Bun runtime optimization with up to 4x faster startup times compared to Node.js
- End-to-end type safety from database schema to API responses using TypeScript
- Automatic OpenAPI/Swagger documentation generation from route definitions
- Eden client for type-safe API consumption with full IntelliSense support
- PostgreSQL JSONB support for hybrid relational-document data models
- Built-in request validation and serialization with compile-time type checking
- Hot reload development server with Bun's native watch mode
- Plugin ecosystem including JWT authentication, CORS, and rate limiting
Common Use Cases
- 1High-performance REST APIs requiring sub-millisecond response times
- 2TypeScript-first applications needing compile-time database query validation
- 3Microservices architecture where fast startup times reduce container orchestration overhead
- 4Real-time applications leveraging PostgreSQL's LISTEN/NOTIFY for pub-sub messaging
- 5Data-intensive applications using PostgreSQL's advanced indexing and query optimization
- 6Enterprise applications requiring ACID compliance and complex transaction management
- 7API-first development with automatic client SDK generation using Eden
Prerequisites
- Minimum 1GB RAM (PostgreSQL requires 256MB + Bun overhead)
- Docker and Docker Compose v2.0 or higher
- Port 3000 available for Elysia web server
- Basic understanding of TypeScript and async/await patterns
- Familiarity with PostgreSQL schema design and SQL queries
- Knowledge of environment variable configuration for database connections
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 elysia: 3 image: oven/bun:latest4 container_name: elysia5 working_dir: /app6 command: bun run src/index.ts7 environment: 8 DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}9 volumes: 10 - ./:/app11 ports: 12 - "3000:3000"13 depends_on: 14 - postgres15 networks: 16 - elysia-network1718 postgres: 19 image: postgres:16-alpine20 environment: 21 POSTGRES_DB: ${DB_NAME}22 POSTGRES_USER: ${DB_USER}23 POSTGRES_PASSWORD: ${DB_PASSWORD}24 volumes: 25 - postgres_data:/var/lib/postgresql/data26 networks: 27 - elysia-network2829volumes: 30 postgres_data: 3132networks: 33 elysia-network: 34 driver: bridge.env Template
.env
1DB_NAME=elysia2DB_USER=elysia3DB_PASSWORD=changemeUsage Notes
- 1Docs: https://elysiajs.com/
- 2Access at http://localhost:3000
- 3End-to-end type safety with Eden client
- 4Optimized for Bun runtime (fastest JS runtime)
- 5Plugin ecosystem: swagger, jwt, cors, etc.
- 6Use Drizzle ORM for type-safe database queries
Individual Services(2 services)
Copy individual services to mix and match with your existing compose files.
elysia
elysia:
image: oven/bun:latest
container_name: elysia
working_dir: /app
command: bun run src/index.ts
environment:
DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
volumes:
- ./:/app
ports:
- "3000:3000"
depends_on:
- postgres
networks:
- elysia-network
postgres
postgres:
image: postgres:16-alpine
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- elysia-network
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 elysia:5 image: oven/bun:latest6 container_name: elysia7 working_dir: /app8 command: bun run src/index.ts9 environment:10 DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}11 volumes:12 - ./:/app13 ports:14 - "3000:3000"15 depends_on:16 - postgres17 networks:18 - elysia-network1920 postgres:21 image: postgres:16-alpine22 environment:23 POSTGRES_DB: ${DB_NAME}24 POSTGRES_USER: ${DB_USER}25 POSTGRES_PASSWORD: ${DB_PASSWORD}26 volumes:27 - postgres_data:/var/lib/postgresql/data28 networks:29 - elysia-network3031volumes:32 postgres_data:3334networks:35 elysia-network:36 driver: bridge37EOF3839# 2. Create the .env file40cat > .env << 'EOF'41DB_NAME=elysia42DB_USER=elysia43DB_PASSWORD=changeme44EOF4546# 3. Start the services47docker compose up -d4849# 4. View logs50docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/elysia-postgres/run | bashTroubleshooting
- Database connection failed: Ensure DATABASE_URL format matches postgresql://user:password@host:port/database and containers are on same network
- Bun command not found: Verify oven/bun:latest image is properly pulled and container has access to /app volume mount
- TypeScript compilation errors: Check that all dependencies are installed with 'bun install' and tsconfig.json is properly configured
- Port 3000 already in use: Change the host port mapping in docker-compose.yml from '3000:3000' to 'XXXX:3000'
- PostgreSQL data persistence issues: Verify postgres_data volume is properly mounted and has correct permissions
- Hot reload not working: Ensure local project files are mounted to /app and Bun's watch mode is enabled in development
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download