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:

```sh npm
npx dinvolt setup
```

```sh bun
bunx dinvolt setup
```

```sh pnpm
pnpm dlx dinvolt setup
```

```sh yarn
yarn dlx dinvolt setup
```

Choose 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:

```dotenv
TURSO_DATABASE_URL=turso://your-database
TURSO_AUTH_TOKEN=your-token
```

PlanetScale Postgres:

```dotenv
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:

```sh npm
npm run db:init
```

```sh bun
bun run db:init
```

```sh pnpm
pnpm run db:init
```

```sh yarn
yarn run db:init
```

Use 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:

```ts
// 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](/guides/data). See the [authentication guide](/guides/authentication) for session and administrator checks.


## In this section

- <span id="change-your-schema"></span><span id="1-edit-the-schema"></span><span id="2-generate-sql"></span><span id="3-apply-the-migration"></span><span id="inspect-the-database"></span>[Database migrations](/guides/database/migrations): Edit your schema, review SQL and apply a migration.
- <span id="database-environments"></span><span id="credential-names"></span><span id="choose-the-matching-command"></span><span id="runtime-selection"></span><span id="deploy-on-vercel"></span><span id="environment-file-loading"></span>[Database environments](/guides/database/environments): Select credentials for local development, staging and production.
- <span id="turso-database"></span>[Turso](/guides/database/turso): Understand the generated Turso driver and connection.
- <span id="planetscale-postgres"></span><span id="connection-pooling"></span>[PlanetScale Postgres](/guides/database/planetscale): Configure application connections, migrations and pooling.
- <span id="initialization-and-troubleshooting"></span><span id="what-initialization-changes"></span><span id="connection-or-initialization-failed"></span><span id="existing-schema-is-incompatible"></span>[Database troubleshooting](/guides/database/troubleshooting): Resolve connection and initial migration problems.

