Passing Kysely instance to betterAuth fails with ERROR [Better Auth]: Failed to initialize database adapter #1427

Closed
opened 2026-03-13 08:39:43 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @rebasecase on GitHub (Jun 27, 2025).

https://kysely.dev/docs/getting-started#instantiation

import { Database } from './types.ts' // this is the Database interface we defined earlier
import { Pool } from 'pg'
import { Kysely, PostgresDialect } from 'kysely'
const pools = new Pool({
    database: 'test',
    host: 'localhost',
    user: 'admin',
    port: 5434,
    max: 10,
  });

const dialect = new PostgresDialect({
  pool
})


const db = new Kysely<Database>({
  dialect,
})

Run something like npx @better-auth/cli@latest generate

// fails
// ERROR [Better Auth]: Failed to initialize database adapter
betterAuth({
  database: db
});

// works but
// WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this.
betterAuth({
  database: dialect
});

// works
betterAuth({
  database: pool
});
Originally created by @rebasecase on GitHub (Jun 27, 2025). https://kysely.dev/docs/getting-started#instantiation ``` import { Database } from './types.ts' // this is the Database interface we defined earlier import { Pool } from 'pg' import { Kysely, PostgresDialect } from 'kysely' const pools = new Pool({ database: 'test', host: 'localhost', user: 'admin', port: 5434, max: 10, }); const dialect = new PostgresDialect({ pool }) const db = new Kysely<Database>({ dialect, }) ``` Run something like `npx @better-auth/cli@latest generate` ``` // fails // ERROR [Better Auth]: Failed to initialize database adapter betterAuth({ database: db }); // works but // WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this. betterAuth({ database: dialect }); // works betterAuth({ database: pool }); ```
Author
Owner

@dosubot[bot] commented on GitHub (Jun 27, 2025):

The reason passing a Kysely instance directly to betterAuth doesn't work as expected is that betterAuth can't auto-detect the database type from a Kysely instance, so it defaults to sqlite and shows a warning. To fix this, you need to pass an object with both your Kysely instance and the explicit type, like this:

betterAuth({
  database: {
    db: db, // your Kysely instance
    type: 'postgres', // or 'mysql', 'sqlite'
  }
});

This pattern is now documented in the project and is the recommended way to integrate Kysely with betterAuth. Passing the underlying pool works because betterAuth can detect the type from the pool instance, but with Kysely you must specify it manually due to type detection limitations. See the recent docs update for a Kysely example and the type definition in the codebase.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Jun 27, 2025): <!-- Answer --> The reason passing a Kysely instance directly to betterAuth doesn't work as expected is that betterAuth can't auto-detect the database type from a Kysely instance, so it defaults to sqlite and shows a warning. To fix this, you need to pass an object with both your Kysely instance and the explicit type, like this: ```ts betterAuth({ database: { db: db, // your Kysely instance type: 'postgres', // or 'mysql', 'sqlite' } }); ``` This pattern is now documented in the project and is the recommended way to integrate Kysely with betterAuth. Passing the underlying pool works because betterAuth can detect the type from the pool instance, but with Kysely you must specify it manually due to type detection limitations. See the [recent docs update for a Kysely example](https://github.com/better-auth/better-auth/pull/3083) and the [type definition in the codebase](https://github.com/better-auth/better-auth/blob/0679ebed3eb12fc86a1c4224b841027e07673638/packages/better-auth/src/types/options.ts). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/fd0c034a-e7e4-4cdf-8caa-37a45ac4569a?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/3194)
Author
Owner

@rebasecase commented on GitHub (Jun 27, 2025):

Good bot!

@rebasecase commented on GitHub (Jun 27, 2025): Good bot!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1427