Database
Connect your database, make your first query, and manage schema changes with Drizzle.
This guide applies to Public website + admin panel and Admin panel only projects.
You can explore the generated site before adding credentials. Database access starts only when server code needs it.
Connect your database
1. Create a database
Create a database with the provider you selected during project creation: Turso Database or PlanetScale Postgres. Dinvolt configures the connection; it does not create the external database service.
2. Run setup
From your generated project’s directory:
npx dinvolt setupbunx dinvolt setuppnpm dlx dinvolt setupyarn dlx dinvolt setupChoose the environment to complete and enter its credentials. Setup checks the connection, initializes a compatible empty database, and offers to create the first administrator.
If the summary reports auth tables ready, you can start using the database. Restart the development server after changing environment variables.
Manual setup
Fill in the provider’s keys in the existing .env.local. Preserve its generated BETTER_AUTH_SECRET.
Turso:
TURSO_DATABASE_URL=turso://your-database
TURSO_AUTH_TOKEN=your-token
PlanetScale Postgres:
DATABASE_URL=postgresql://app-user:password@your-host:6432/your-database?sslmode=verify-full
DATABASE_URL_DIRECT=postgresql://migration-user:password@your-host:5432/your-database?sslmode=verify-full
Review the initial SQL already generated in drizzle/, then initialize an empty development database:
npm run db:initbun run db:initpnpm run db:inityarn run db:initUse your project’s package manager in place of Bun. The examples on this page target development unless their command includes another environment.
Query from server code
Import the lazy database client from @/db. It is reused across requests and development reloads.
For example, projects with authentication already have a user table. This server helper selects only the fields it needs:
// src/lib/find-user.ts
import "server-only";
import { eq } from "drizzle-orm";
import { getDb } from "@/db";
import { user } from "@/db/schema";
export async function findUser(id: string) {
const [result] = await getDb()
.select({ id: user.id, name: user.name })
.from(user)
.where(eq(user.id, id))
.limit(1);
return result ?? null;
}
Authorize the caller before returning private data from a page, action, or API. Keep database code in the Node.js server runtime.
For client requests, use an authorized tRPC procedure. See the authentication guide for session and administrator checks.
In this section
- Database migrations: Edit your schema, review SQL and apply a migration.
- Database environments: Select credentials for local development, staging and production.
- Turso: Understand the generated Turso driver and connection.
- PlanetScale Postgres: Configure application connections, migrations and pooling.
- Database troubleshooting: Resolve connection and initial migration problems.