LogoLaunchSaaS

Database

Set up PostgreSQL with Drizzle ORM in LaunchSaaS. Connect to Neon, Supabase, or self-hosted databases with type-safe queries and migrations.

Database

LaunchSaaS uses PostgreSQL with Drizzle ORM for type-safe database operations. This guide covers database configuration and setup.

Setup

Create a Database

Recommended to use hosted PostgreSQL database services. They provide easy setup and management, and often include free tiers sufficient for getting started.

Neon is a serverless PostgreSQL service with an excellent developer experience.

Setup Steps:

  1. Create an account at neon.tech
  2. Create a new project
  3. Create a database
  4. Get your connection string from the dashboard
  5. Add the connection string to your .env file:
DATABASE_URL="postgres://user:[email protected]/database?sslmode=require"

Supabase

Supabase provides PostgreSQL databases with additional features like authentication and storage.

Setup Steps:

  1. Create an account at supabase.com
  2. Create a new project
  3. Click on the Connect button
  4. Get your connection string in Transaction pooler section
  5. Add the connection string to your .env file:
DATABASE_URL="postgres://postgres:[email protected]:6543/postgres"

Initialize the Database

After configuring your database URL, initialize the database:

pnpm run init

This command will:

  1. Generate the Drizzle migrations
  2. Apply migrations to your database
  3. Create the admin user

If you are setting up the environment, now you can go back to the Environment Setup guide and continue.

Other Database Options

Self-Hosted

Docker

Run PostgreSQL in a Docker container for local development:

docker run --name launchsaas-postgres -e POSTGRES_PASSWORD=mypassword -d -p 5432:5432 postgres

Connection string:

DATABASE_URL="postgres://postgres:mypassword@localhost:5432/postgres"

Local Installation

Install PostgreSQL directly on your machine from postgres.org.

Other Hosted Services

Database Commands

CommandDescription
pnpm run db:generateGenerate migrations from schema changes
pnpm run db:migrateApply migrations to database
pnpm run db:pushPush schema changes directly (dev only, skips migrations)
pnpm run db:generate:authRegenerate Better Auth schema (after auth config changes)
pnpm run init:dbGenerate + migrate (combined)

Running Commands for Different Environments

By default, database commands use your .env file. To run commands against a different environment (e.g., production or staging), use dotenv to load the appropriate environment file:

# Push schema to production database
pnpm dotenv -e .env.production -- pnpm db:push

# Run migrations on staging database
pnpm dotenv -e .env.staging -- pnpm db:migrate

# Generate migrations using production database
pnpm dotenv -e .env.production -- pnpm db:generate

Be careful when running database commands against production. Always backup your data before running migrations, and prefer db:migrate over db:push in production environments.

Schema Organization

Database schemas are located in src/schemas/tables/:

FileDescription
auth.tsAuthentication tables (auto-generated by Better Auth)
order.tsOrder table for confirmed monetary transactions
entitlement.tsEntitlement table for user access to products
index.tsRe-exports all schemas

Don't manually edit auth.ts - it's auto-generated. Run npm run db:generate:auth after changing auth configuration.

Order Table

Represents confirmed monetary transactions:

  • Created when payment is confirmed
  • One-time products create an order upon checkout completion
  • Subscription products create an order upon each successful payment (recurring orders)
  • Contains: provider info, product details, amount, currency, metadata

Entitlement Table

Represents user access to products:

  • Granted/extended when order is paid
  • One-time products create entitlement with status "active" upon checkout
  • Subscription products create entitlement with status "pending", becomes "active" after first payment
  • Contains: subscription period info, cancellation status, provider subscription IDs
  • Supports multiple payment providers (Stripe, Creem, etc.)

FAQ

Connection Issues

If you're having trouble connecting to your database:

  1. Check that your DATABASE_URL is correctly formatted
  2. Ensure your IP is allowed in the database's firewall settings
  3. Verify that the database user has the correct permissions
  4. Check for any network restrictions

Migration Issues

If you're experiencing issues with database migrations:

  1. Check your schema definitions for errors
  2. Ensure your migration scripts are correctly formatted
  3. Try running migrations manually to see detailed errors

References

Next Steps