Skip to main content

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 .env file in your project root:
.env
For cloud databases, use the connection string from your provider:
Add .env to your .gitignore to keep credentials secure.
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 drizzle.config.ts in your project root:
drizzle.config.ts
This tells Drizzle Kit where to find your schema and how to connect to your database.
5

Generate and run migrations

Generate SQL migrations from your schema:
This creates migration files in the ./drizzle directory. Apply them to your database:
During development, you can use npx drizzle-kit push to apply schema changes directly without generating migration files.
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.
Add a script to your package.json for convenience:

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:
This opens a web interface at 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

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=require for cloud databases)
Make sure your schema types are correctly imported:
Restart your TypeScript server in VSCode: Cmd+Shift+P > “Restart TS Server”
If migrations fail:
  1. Check drizzle.config.ts points to the correct schema file
  2. Verify database credentials
  3. Review generated SQL in ./drizzle folder
  4. Run npx drizzle-kit drop to reset (development only)
Need help? Join our Discord community or check out GitHub Discussions for support.