---
title: "PostgreSQL with Drizzle ORM"
description: "Nuxt Starter Kit uses PostgreSQL with Drizzle ORM. Each layer owns its tables, drizzle-kit generates the migrations, and any PostgreSQL provider works."
canonical_url: "https://nuxtstarterkit.com/docs/database"
---

> Index of every page on this site: [llms.txt](https://nuxtstarterkit.com/llms.txt)

The starter kit uses **PostgreSQL** as the database, managed through **Drizzle ORM** for excellent developer experience and type safety.

## Database Provider

You can use PostgreSQL from various providers:

- **Neon** - Serverless PostgreSQL with generous free tier
- **Supabase** - Open source Firebase alternative with PostgreSQL
- **Railway** - Simple PostgreSQL hosting
- **Render** - Managed PostgreSQL databases
- **Local PostgreSQL** - For development

## Drizzle ORM

The starter kit uses [Drizzle ORM](https://orm.drizzle.team/) instead of raw SQL for several benefits:

- **Type safety** - Full TypeScript support with auto-completion
- **Migrations** - Automated schema migrations
- **Relations** - Easy relationship management
- **Developer experience** - Intuitive query builder

## Database Schema

Every layer owns its tables in `server/db/schema/*.ts`:

<table>
<thead>
  <tr>
    <th>
      Table file
    </th>
    
    <th>
      Layer
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        auth.ts
      </code>
      
       - user, session, account, verification, subscription, passkey (better-auth)
    </td>
    
    <td>
      <code>
        apps/web/layers/auth
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        file.ts
      </code>
      
       - File storage metadata
    </td>
    
    <td>
      <code>
        apps/web/layers/storage
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        testimonial.ts
      </code>
      
       - Testimonials
    </td>
    
    <td>
      <code>
        apps/web/layers/testimonials
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        waitlist.ts
      </code>
      
       - Waitlist signups
    </td>
    
    <td>
      <code>
        apps/web/layers/waitlist
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        banner.ts
      </code>
      
       - App banners
    </td>
    
    <td>
      <code>
        apps/web/layers/banner
      </code>
    </td>
  </tr>
</tbody>
</table>

drizzle-kit collects the schema from `apps/web/layers/*/server/db/schema/*.ts`. The configuration is in `apps/web/layers/auth/server/db/drizzle.config.ts`, and all migrations are in `apps/web/layers/auth/server/db/migrations/`.

When you delete an optional layer, `pnpm db:generate` creates a migration that drops its tables.

## Database Utilities

`useDb()` is auto-imported in server code. Import the tables of your own layer with a relative path:

```typescript
// apps/web/layers/testimonials/server/utils/useTestimonials.ts
import { desc } from 'drizzle-orm'

import { testimonial } from '../db/schema/testimonial'

const db = useDb()
const testimonials = await db.select()
  .from(testimonial)
  .orderBy(desc(testimonial.createdAt))
```

## Commands

Run these commands from the **repository root**:

```bash
# Generate migrations from schema changes
pnpm db:generate

# Apply migrations to database
pnpm db:migrate

# Open Drizzle Studio (database GUI)
pnpm db:studio
```

## Environment Variables

Set your PostgreSQL connection string in `apps/web/.env`. drizzle-kit reads it from there, too:

```bash
NUXT_PRIVATE_DATABASE_URL="postgresql://user:password@host:5432/dbname"
```

For local development, you might use:

```bash
NUXT_PRIVATE_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/nuxtstarterkit"
```

## Migration Workflow

1. Change or add a schema file in `server/db/schema/` of the layer the table belongs to
2. Run `pnpm db:generate` to create migration files
3. Review generated migrations in `apps/web/layers/auth/server/db/migrations/`
4. Run `pnpm db:migrate` to apply migrations
5. Commit both schema and migration files

## Drizzle Studio

Drizzle Studio provides a visual interface to browse and edit your database:

```bash
pnpm db:studio
```

This opens a web interface where you can:

- Browse tables and data
- Run queries
- Edit records
- View relationships

## Type Safety

Drizzle infers TypeScript types from your schema. `User` and `Session` are auto-imported from `apps/web/layers/auth/shared/types/db.ts`, and each feature layer exports its types from `shared/types/`:

```typescript
// apps/web/layers/auth/shared/types/db.ts
import type { session, user } from '../../server/db/schema/auth'

export type User = typeof user.$inferSelect
export type Session = typeof session.$inferSelect
```

Tables of another layer are imported with a relative path, because drizzle-kit doesn't resolve aliases:

```typescript
// apps/web/layers/storage/server/db/schema/file.ts
import { user } from '../../../../auth/server/db/schema/auth'
```

## Best Practices

- Always use migrations - never modify the database schema directly
- Keep schema files organized by feature
- Use meaningful column and table names
- Add indexes for frequently queried columns
- Use transactions for multiple related operations

## Sitemap

See the full [sitemap](https://nuxtstarterkit.com/sitemap.md) for all pages.
