Build your first app
This guide will walk you through creating a complete database application with Drizzle ORM, from installation to executing your first queries.We’ll use PostgreSQL with node-postgres driver in this guide, but Drizzle works the same way with MySQL and SQLite.
Prerequisites
Before starting, ensure you have:- Node.js 18+ or Bun installed
- A PostgreSQL database running (or use a cloud provider like Neon, Supabase, or Vercel)
- Basic TypeScript knowledge
Step-by-step tutorial
1
Install dependencies
Create a new project and install Drizzle ORM with the PostgreSQL driver:
drizzle-orm: Core ORM librarypg: PostgreSQL driverdrizzle-kit: CLI for migrationstsx: TypeScript execution runtime
2
Set up environment variables
Create a For cloud databases, use the connection string from your provider:
.env file in your project root:.env
- Neon
- Supabase
- Vercel Postgres
3
Define your database schema
Create
src/db/schema.ts and define your tables:src/db/schema.ts
Relations are optional but enable powerful relational queries. You can skip them and use SQL joins instead.
4
Configure Drizzle Kit
Create This tells Drizzle Kit where to find your schema and how to connect to your database.
drizzle.config.ts in your project root:drizzle.config.ts
5
Generate and run migrations
Generate SQL migrations from your schema:This creates migration files in the
./drizzle directory. Apply them to your database:6
Initialize database connection
Create
src/db/index.ts to set up your database connection:src/db/index.ts
Passing the schema enables relational queries via
db.query. If you only need SQL-like queries, you can omit it: drizzle(pool)7
Execute your first queries
Create
src/index.ts to test your setup:src/index.ts
8
Run your application
Execute your TypeScript file:You should see output showing the created user and post, followed by the query results.
Query examples
Now that you have a working setup, explore common query patterns:Insert operations
Select queries
Update operations
Delete operations
Joins and aggregations
Explore Drizzle Studio
Drizzle Kit includes a visual database browser:https://local.drizzle.studio where you can:
- Browse all tables and data
- Execute queries visually
- Edit records directly
- View table relationships
Drizzle Studio is perfect for development and debugging. It reads your schema file for full type information.
Database-specific guides
This quickstart used PostgreSQL, but Drizzle works identically with other databases:MySQL
Complete guide for MySQL and MariaDB
SQLite
Guide for SQLite, including D1 and Turso
All Drivers
Overview of all supported database drivers
Next steps
Now that you have a working application, dive deeper into Drizzle’s features:Schema Declaration
Learn advanced schema features: constraints, indexes, custom types
Queries
Master complex queries, joins, and aggregations
Relations
Build type-safe relational queries
Migrations
Advanced migration workflows and strategies
Transactions
Execute atomic operations with transactions
Best Practices
Production-ready patterns and optimizations
Common patterns
Connection pooling
For production applications, configure connection pooling:Environment-based configuration
Manage different database configurations per environment:src/db/config.ts
Prepared statements
Optimize repeated queries with prepared statements:Troubleshooting
Connection refused error
Connection refused error
Ensure your database is running and the connection string is correct. Check:
- Database host and port
- Username and password
- Database name
- SSL requirements (add
?sslmode=requirefor cloud databases)
Type errors in queries
Type errors in queries
Make sure your schema types are correctly imported:Restart your TypeScript server in VSCode: Cmd+Shift+P > “Restart TS Server”
Migration errors
Migration errors
If migrations fail:
- Check
drizzle.config.tspoints to the correct schema file - Verify database credentials
- Review generated SQL in
./drizzlefolder - Run
npx drizzle-kit dropto reset (development only)
Need help? Join our Discord community or check out GitHub Discussions for support.