[GH-ISSUE #7926] getPostgresSchema always returs 'public' #28270

Closed
opened 2026-04-17 19:42:54 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @ismael-iskauskas on GitHub (Feb 11, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/7926

Originally assigned to: @bytaesu on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

// useAuthDatabase returns a Kysely Database with the Pool

authPoolInstance = new Pool({
      host: 'localhost',
      port: 5432,
      user: 'REDACTED',
      password: 'READACTED',
      database: 'MY_DATABASE_NAME',
      options: '-c search_path=auth'
    });

export function createAuth() {
   return betterAuth({
    database: {
      db: useAuthDatabase(),
      type: 'postgres',
      casing: 'snake',
      debugLogs: true,
      transaction: false,
    },
    logger: {
      level: 'debug',
    },
    emailAndPassword: {
      enabled: true,
    },
    plugins: [
      username(),
    ],
    user: {
      modelName: 'users',
    },
    session: {
      modelName: 'sessions',
    },
    account: {
      modelName: 'accounts',
    },
    verification: {
      modelName: 'verifications',
    },
  });
};

export const auth = createAuth();

const { toBeCreated, toBeAdded, compileMigrations, runMigrations } = await getMigrations(auth.options);

Poduces log
2026-02-11T15:11:01.472Z DEBUG [Better Auth]: PostgreSQL migration: Using schema 'public' (from search_path)
2026-02-11T15:11:01.495Z DEBUG [Better Auth]: Found 0 table(s) in schema 'public': (none)

Current vs. Expected behavior

It should use the provided search_path but it will always use public

What version of Better Auth are you using?

1.4.18

System info

{
  "system": {
    "platform": "win32",
    "arch": "x64",
    "version": "Windows 11 Pro",
    "release": "10.0.26300",
    "cpuCount": 16,
    "cpuModel": "Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz",
    "totalMemory": "63.92 GB",
    "freeMemory": "34.70 GB"
  },
  "node": {
    "version": "v24.13.1",
    "env": "development"
  },
  "packageManager": {
    "name": "bun",
    "version": "1.3.8"
  },
  "frameworks": null,
  "databases": [
    {
      "name": "pg",
      "version": "catalog:core"
    },
    {
      "name": "kysely",
      "version": "^0.28.11"
    }
  ],
  "betterAuth": {
    "version": "Unknown",
    "config": null,
    "error": "Couldn't read your auth config. Make sure to default export your auth instance or to export as a variable named auth."
  }
}

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

I found the Bug in

async function getPostgresSchema(db) {
  try {
    const result = await sql`SHOW search_path`.execute(db);
    console.log(result.rows[0]);
// Output of console log
//  {
//    searchPath: "auth",
//  }
    if (result.rows[0]?.search_path) //<-- Here it is checked for search_path but the value is in searchPath
      return (
        result.rows[0].search_path
          .split(',')
          .map((s) => s.trim())
          .map((s) => s.replace(/^["']|["']$/g, ''))
          .filter((s) => !s.startsWith('$'))[0] || 'public'
      );
  } catch {}
  return 'public';
}

Originally created by @ismael-iskauskas on GitHub (Feb 11, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/7926 Originally assigned to: @bytaesu on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce ``` // useAuthDatabase returns a Kysely Database with the Pool authPoolInstance = new Pool({ host: 'localhost', port: 5432, user: 'REDACTED', password: 'READACTED', database: 'MY_DATABASE_NAME', options: '-c search_path=auth' }); export function createAuth() { return betterAuth({ database: { db: useAuthDatabase(), type: 'postgres', casing: 'snake', debugLogs: true, transaction: false, }, logger: { level: 'debug', }, emailAndPassword: { enabled: true, }, plugins: [ username(), ], user: { modelName: 'users', }, session: { modelName: 'sessions', }, account: { modelName: 'accounts', }, verification: { modelName: 'verifications', }, }); }; export const auth = createAuth(); const { toBeCreated, toBeAdded, compileMigrations, runMigrations } = await getMigrations(auth.options); ``` Poduces log 2026-02-11T15:11:01.472Z DEBUG [Better Auth]: PostgreSQL migration: Using schema 'public' (from search_path) 2026-02-11T15:11:01.495Z DEBUG [Better Auth]: Found 0 table(s) in schema 'public': (none) ### Current vs. Expected behavior It should use the provided search_path but it will always use public ### What version of Better Auth are you using? 1.4.18 ### System info ```bash { "system": { "platform": "win32", "arch": "x64", "version": "Windows 11 Pro", "release": "10.0.26300", "cpuCount": 16, "cpuModel": "Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz", "totalMemory": "63.92 GB", "freeMemory": "34.70 GB" }, "node": { "version": "v24.13.1", "env": "development" }, "packageManager": { "name": "bun", "version": "1.3.8" }, "frameworks": null, "databases": [ { "name": "pg", "version": "catalog:core" }, { "name": "kysely", "version": "^0.28.11" } ], "betterAuth": { "version": "Unknown", "config": null, "error": "Couldn't read your auth config. Make sure to default export your auth instance or to export as a variable named auth." } } ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context I found the Bug in ```typescript async function getPostgresSchema(db) { try { const result = await sql`SHOW search_path`.execute(db); console.log(result.rows[0]); // Output of console log // { // searchPath: "auth", // } if (result.rows[0]?.search_path) //<-- Here it is checked for search_path but the value is in searchPath return ( result.rows[0].search_path .split(',') .map((s) => s.trim()) .map((s) => s.replace(/^["']|["']$/g, '')) .filter((s) => !s.startsWith('$'))[0] || 'public' ); } catch {} return 'public'; } ```
GiteaMirror added the lockedbug labels 2026-04-17 19:42:54 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Feb 11, 2026):

It looks like your issue with getPostgresSchema always returning 'public' could be due to how the search path is being checked in the getPostgresSchema function. The function currently checks for search_path, but the actual value returned from PostgreSQL might be in a property named searchPath.

Here's what you can try to troubleshoot:

  1. Modify the Schema Check: Update the getPostgresSchema function to correctly access the searchPath property instead. You could adjust the line checking for result.rows[0]?.search_path to use searchPath instead. This alteration could help in retrieving the correct schema.

  2. Check Database Connection Options: Ensure that you're configuring the connection pool with the correct options parameter to set search_path. You should have something like this in your connection setup:

    options: '-c search_path=auth'
    

    This part looks correct based on your provided details.

  3. Potential Database User Default Schema: If changing the schema in the connection options doesn’t work, consider setting the default schema for the database user. You might run a command like:

    ALTER USER your_user SET search_path TO auth;
    

    This sets the user's default to your desired schema.

If these suggestions do not resolve the issue, please tag @bekacru or @ping__ for further assistance. They are part of the Better Auth team and can provide you with more specific guidance.

What have you tried so far in troubleshooting this issue?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3885164191 --> @better-auth-agent[bot] commented on GitHub (Feb 11, 2026): It looks like your issue with `getPostgresSchema` always returning 'public' could be due to how the search path is being checked in the `getPostgresSchema` function. The function currently checks for `search_path`, but the actual value returned from PostgreSQL might be in a property named `searchPath`. Here's what you can try to troubleshoot: 1. **Modify the Schema Check**: Update the `getPostgresSchema` function to correctly access the `searchPath` property instead. You could adjust the line checking for `result.rows[0]?.search_path` to use `searchPath` instead. This alteration could help in retrieving the correct schema. 2. **Check Database Connection Options**: Ensure that you're configuring the connection pool with the correct `options` parameter to set `search_path`. You should have something like this in your connection setup: ```javascript options: '-c search_path=auth' ``` This part looks correct based on your provided details. 3. **Potential Database User Default Schema**: If changing the schema in the connection options doesn’t work, consider setting the default schema for the database user. You might run a command like: ```sql ALTER USER your_user SET search_path TO auth; ``` This sets the user's default to your desired schema. If these suggestions do not resolve the issue, please tag @bekacru or @ping__ for further assistance. They are part of the Better Auth team and can provide you with more specific guidance. What have you tried so far in troubleshooting this issue? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) • [Discord](https://discord.gg/better-auth) • [GitHub](https://github.com/better-auth/better-auth) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/better-auth_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/better-auth) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/better-auth/better-auth)
Author
Owner

@ismael-iskauskas commented on GitHub (Feb 11, 2026):

@Bekacru i tried all the proposed solutions.

Its always using public schema because of the access of serach_path instead of searchPath

<!-- gh-comment-id:3885179345 --> @ismael-iskauskas commented on GitHub (Feb 11, 2026): @Bekacru i tried all the proposed solutions. Its always using public schema because of the access of serach_path instead of searchPath
Author
Owner

@ismael-iskauskas commented on GitHub (Feb 11, 2026):

Ok. More research done. This happens because of the CamelCasePlugin from Kysely.

I dont know if there is a good workaround. I will for now disable the plugin for better-auth

<!-- gh-comment-id:3885744241 --> @ismael-iskauskas commented on GitHub (Feb 11, 2026): Ok. More research done. This happens because of the CamelCasePlugin from Kysely. I dont know if there is a good workaround. I will for now disable the plugin for better-auth
Author
Owner

@bytaesu commented on GitHub (Feb 22, 2026):

Hi @ismael-iskauskas, I'm looking into this 🙂

<!-- gh-comment-id:3939740850 --> @bytaesu commented on GitHub (Feb 22, 2026): Hi @ismael-iskauskas, I'm looking into this 🙂
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#28270