Skip to content
← All posts

·

How to set up a Postgres database for your web app

Pick a host, choose pooled or direct URLs, define a schema and run migrations for your app. The manual steps, and what Launchfast does for you.

Short answer: create a hosted Postgres database, put its connection string in DATABASE_URL, describe your tables in code, and apply them with a migration tool. Use a pooled connection for the app and a direct one for migrations if your host offers both. Launchfast already ships a Neon + Drizzle schema with migrations, and npm run setup creates and connects the database for you.

What are you actually setting up?

A web app needs somewhere to keep users, sessions and orders. That means a Postgres server you can reach from your laptop and from your hosting platform, a schema, and a repeatable way to change that schema as the app grows.

The pain usually shows up as one of these:

  • Error: connection string is undefined or DATABASE_URL is not set when the build runs.
  • getaddrinfo ENOTFOUND when your machine can't resolve the database host (see Neon database not connecting on Jio Wi-Fi).
  • relation "user" does not exist because the tables were never created.
  • Too many connections once the app runs as serverless functions.

How do you do it by hand?

  1. Pick a host. Neon, Supabase, RDS and others all work. For a serverless deployment, a host with a pooler or an HTTP driver is easier, because each request can open its own connection.
  2. Create the project and pick a region. Choose one close to where your server functions run. Every query is a network round trip, so a database on the other side of the world is slow on every page.
  3. Copy the connection string. It looks like postgresql://user:password@host/dbname?sslmode=require. Put it in .env.local as DATABASE_URL and never commit it.
  4. Decide pooled or direct. A pooled string goes through a connection pooler (on Neon, the hostname contains -pooler). That suits many short-lived serverless connections. A direct string connects to Postgres itself, which is what migration tools and anything that relies on session state expect. Keep both strings and use them for the right job.
  5. Install a driver and an ORM. For example npm i drizzle-orm @neondatabase/serverless and npm i -D drizzle-kit.
  6. Write the schema. Every table, column, index and foreign key, in TypeScript if you use Drizzle.
  7. Configure the migration tool. A drizzle.config.ts that points at the schema and reads DATABASE_URL.
  8. Generate and apply migrations. npx drizzle-kit generate, then npx drizzle-kit migrate. Commit the generated SQL, and never edit a migration that has already been applied.
  9. Create the client without breaking the build. If you call neon(process.env.DATABASE_URL!) at import time, next build fails on any machine that has no database yet. Create the client on first use instead.
  10. Repeat on production. Set DATABASE_URL in your hosting dashboard, run the migrations against the production database, and check that your host's function region is near the database.

The traps are in the details: the wrong URL for migrations, a missing sslmode, a DNS lookup that fails on your network, a client created too early, and a schema you edit directly in the console so that it no longer matches your code.

What does Launchfast already do?

Launchfast uses Neon Postgres with Drizzle. The pieces are all in the repository.

The schema exists. src/db/schema.ts defines the tables the kit needs: user, session, account, verification and rate_limit for Better Auth, plus billing_customer, subscription, payment and repo_access for billing. The auth tables use the field names Better Auth expects. Money is stored as integers in the smallest currency unit.

The migrations are written. The drizzle/ folder holds four SQL migrations (0000_init.sql to 0003_repo_access.sql), so a fresh database gets every table with one command.

The client doesn't break your build. src/db/index.ts connects on first use, so next build works before DATABASE_URL is set:

export const db = new Proxy({} as Db, {
  get(_, key) {
    instance ??= drizzle(neon(requireEnv("DATABASE_URL")), { schema });
    const value = Reflect.get(instance, key);
    return typeof value === "function" ? value.bind(instance) : value;
  },
});

The migration tool is configured. drizzle.config.ts reads DATABASE_URL from .env.local, and package.json has npm run db:generate and npm run db:migrate. When you change src/db/schema.ts, generate a new migration instead of editing an old one.

npm run setup does the database step. The setup script in scripts/setup/ asks for a Neon API key, lets you pick a region (Singapore, N. Virginia or Frankfurt), creates the project through the Neon API and saves the connection string as DATABASE_URL in .env.local. Then it checks that your machine can resolve the database hostname. If it can't, it prints the DNS fix instead of leaving you with a hanging migration. After that it runs drizzle-kit migrate to create the tables.

npm run setup
# Database → Creating your database → Creating tables… → Database ready.

When you later deploy, the setup script also reads the database host to pick a Vercel function region next to it and saves it in vercel.json.

What should you know before relying on this?

The kit connects with Neon's serverless driver over HTTP, so there is no long-lived connection pool to manage in your own code. If you move to a different host, you would change src/db/index.ts and the setup step. The schema and migrations are plain Drizzle and Postgres, so they carry over.

How much does this save?

You skip choosing a driver, writing the auth and billing schema, wiring migrations, and chasing the first relation does not exist error. You run one command and the tables are there.

Launchfast is the starter this post describes. If you are working through launch tasks, read the checklist I run before launching a SaaS and Rebrand a SaaS starter with Claude Code next.