Skip to content

CLI generate command: Support for PostgreSQL custom schema (pgSchema) in Drizzle output #6606

@alexander-zuev

Description

@alexander-zuev

Problem

When using a custom PostgreSQL schema (e.g., auth instead of public), the CLI generate command outputs Drizzle schema using pgTable():

export const user = pgTable("user", {...});

Instead of using Drizzle's pgSchema() for explicit schema qualification:

const authSchema = pgSchema("auth");
export const user = authSchema.table("user", {...});

Current Workaround

The docs recommend using search_path at connection time:

connectionString: "postgres://...?options=-c search_path=auth"

This works for runtime queries, but the generated Drizzle schema doesn't reflect the actual database structure. This causes confusion and potential issues with drizzle-kit migrations.

Expected Behavior

If Better Auth detects a custom search_path in the database connection (or via a CLI flag), the generate command should output:

import { pgSchema } from "drizzle-orm/pg-core";

const authSchema = pgSchema("auth");

export const user = authSchema.table("user", {...});
export const session = authSchema.table("session", {...});
// etc.

Proposed Solutions

  1. Auto-detect: If the CLI config has a real database connection, detect search_path and generate accordingly
  2. CLI flag: Add --pg-schema auth flag to explicitly specify the schema
  3. Config option: Add schema: "auth" option to the Drizzle adapter config

Environment

  • Better Auth version: 1.4.x
  • Database: PostgreSQL
  • ORM: Drizzle

Related Docs

The docs mention custom schema support, but it only applies to migrate (Kysely) and runtime queries, not the generate command output for Drizzle users.

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseDatabase layer, all adapters, schema, migrationsdevtoolsCLI, OpenAPI, telemetry, i18n, test-utils

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions