Custom Database
Use this guide when you want Better Auth to run against your existing database stack instead of NuxtHub.
Better Auth supports any database through adapters. Use this guide when you need full control over your database setup or can't use NuxtHub.
When to Use This
| Scenario | Recommended |
|---|---|
| Quick start, managed infra | NuxtHub |
| OAuth-only, no persistence | Database-less Mode |
| Existing database, custom setup | This guide |
Install Better Auth
Adapter imports come from the better-auth package, so you must install it in your app.
pnpm add better-auth
better-auth is a peer dependency of @onmax/nuxt-better-auth. Keep the major versions aligned to avoid mismatches.
Drizzle Adapter
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { drizzle } from 'drizzle-orm/node-postgres'
const db = drizzle(process.env.DATABASE_URL!)
export default defineServerAuth({
database: drizzleAdapter(db, { provider: 'pg' }),
emailAndPassword: { enabled: true },
})
Prisma Adapter
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { prismaAdapter } from 'better-auth/adapters/prisma'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export default defineServerAuth({
database: prismaAdapter(prisma, { provider: 'postgresql' }),
emailAndPassword: { enabled: true },
})
Kysely Adapter
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'
import { kyselyAdapter } from 'better-auth/adapters/kysely'
import { Kysely, PostgresDialect } from 'kysely'
import { Pool } from 'pg'
const db = new Kysely({
dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
})
export default defineServerAuth({
database: kyselyAdapter(db, { dialect: 'postgres', type: 'pg' }),
emailAndPassword: { enabled: true },
})
Schema Setup
You must create the required tables manually. Better Auth provides a CLI to generate schemas:
npx @better-auth/cli@latest generate
This generates migration files for your adapter. Apply them with your database tool.
CLI config (Nuxt)
The CLI reads a Better Auth instance from auth.ts, not your server/auth.config.ts. Add a dedicated auth.ts file for CLI generation.
import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { drizzle } from 'drizzle-orm/node-postgres'
const db = drizzle(process.env.DATABASE_URL!)
export const auth = betterAuth({
database: drizzleAdapter(db, { provider: 'pg' }),
emailAndPassword: { enabled: true },
})
If you use Prisma or Kysely, swap the adapter section to match your setup.
Use --config if you place the file elsewhere:
npx @better-auth/cli@latest generate --config server/auth/auth.ts
Keep this config in sync with server/auth.config.ts so the generated schema matches your runtime plugins and options.
Environment Variables
Store your connection string securely:
DATABASE_URL="postgresql://user:password@localhost:5432/myapp"