[GH-ISSUE #6606] CLI generate command: Support for PostgreSQL custom schema (pgSchema) in Drizzle output #10568

Open
opened 2026-04-13 06:47:47 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @alexander-zuev on GitHub (Dec 8, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6606

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

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

Originally created by @alexander-zuev on GitHub (Dec 8, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6606 ## Problem When using a custom PostgreSQL schema (e.g., `auth` instead of `public`), the CLI `generate` command outputs Drizzle schema using `pgTable()`: ```typescript export const user = pgTable("user", {...}); ``` Instead of using Drizzle's `pgSchema()` for explicit schema qualification: ```typescript const authSchema = pgSchema("auth"); export const user = authSchema.table("user", {...}); ``` ## Current Workaround The docs recommend using `search_path` at connection time: ```typescript 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: ```typescript 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 - [PostgreSQL Adapter - Non-default schema](https://www.better-auth.com/docs/adapters/postgresql#use-a-non-default-schema) - [CLI - Generate command](https://www.better-auth.com/docs/concepts/cli) The docs mention custom schema support, but it only applies to `migrate` (Kysely) and runtime queries, not the `generate` command output for Drizzle users.
GiteaMirror added the databasedevtools labels 2026-04-13 06:47:47 -05:00
Author
Owner

@rieger-jared commented on GitHub (Dec 24, 2025):

I would personally prefer the schema option in the auth configuration!

<!-- gh-comment-id:3688351598 --> @rieger-jared commented on GitHub (Dec 24, 2025): I would personally prefer the schema option in the auth configuration!
Author
Owner

@SaizFerri commented on GitHub (Jan 19, 2026):

+1 to this. ideally passing the schema as a config option on the database adapter.

<!-- gh-comment-id:3769588132 --> @SaizFerri commented on GitHub (Jan 19, 2026): +1 to this. ideally passing the schema as a config option on the database adapter.
Author
Owner

@pekastel commented on GitHub (Jan 26, 2026):

+1

<!-- gh-comment-id:3799713692 --> @pekastel commented on GitHub (Jan 26, 2026): +1
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#10568