Using Cloudflare Hyperdrive Postgres connection string causes a timeout #1042

Closed
opened 2026-03-13 08:20:10 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @stuartwk on GitHub (Apr 14, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  • Create a Postgres DB
  • Create a boilerplate project using better-auth, and launch to Cloudflare workers.
  • In Cloudflare, create a Hyperdrive instance, using that Postgres connection.
  • Associate the Hyperdrive instance with your Cloudflare worker.
  • Try to call something like sign-in with the Hyperdrive connection string. It hangs indefinitely. Now if you swap out the Hyperdrive connectionString with the normal Postgres connectionString, it works.

Current vs. Expected behavior

I expect the Hyperdrive connectionString to work like the regular Postgres connectionString.

What version of Better Auth are you using?

1.2.6

Provide environment information

- OS: MacOS 15.4
- Browser: Brave, but I'm using better-auth all server-side.

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

Backend

Auth config (if applicable)

import postgres from "postgres";
import { PostgresJSDialect } from "kysely-postgres-js";
import { BETTER_AUTH_SECRET } from "astro:env/server";

export const auth = (env: Env) =>
  betterAuth({
    database: {
      dialect: new PostgresJSDialect({
        postgres: getDb(env), // Call getDb() as a function to create a new connection each time
      }),
      // dialect,
      type: "postgres",
    },
    emailAndPassword: {
      enabled: true,
    },
    secret: BETTER_AUTH_SECRET,
  });

export function getDb(env: Env) {
  // Connection URL from environment variables
  let connectionString = env.HYPERDRIVE.connectionString;

  const sql = postgres(connectionString, {
    max: 5,
    // idle_timeout: 30,
    ssl: import.meta.env.DEV,
    // Debug options - remove in production
    debug: true,
    fetch_types: false,
  });

  return sql;
}

Additional context

Unfortunately, it's only reproducible in a live Cloudflare environment.

Originally created by @stuartwk on GitHub (Apr 14, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce - Create a Postgres DB - Create a boilerplate project using better-auth, and launch to Cloudflare workers. - In Cloudflare, create a Hyperdrive instance, using that Postgres connection. - Associate the Hyperdrive instance with your Cloudflare worker. - Try to call something like sign-in with the Hyperdrive connection string. It hangs indefinitely. Now if you swap out the Hyperdrive connectionString with the normal Postgres connectionString, it works. ### Current vs. Expected behavior I expect the Hyperdrive connectionString to work like the regular Postgres connectionString. ### What version of Better Auth are you using? 1.2.6 ### Provide environment information ```bash - OS: MacOS 15.4 - Browser: Brave, but I'm using better-auth all server-side. ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import postgres from "postgres"; import { PostgresJSDialect } from "kysely-postgres-js"; import { BETTER_AUTH_SECRET } from "astro:env/server"; export const auth = (env: Env) => betterAuth({ database: { dialect: new PostgresJSDialect({ postgres: getDb(env), // Call getDb() as a function to create a new connection each time }), // dialect, type: "postgres", }, emailAndPassword: { enabled: true, }, secret: BETTER_AUTH_SECRET, }); export function getDb(env: Env) { // Connection URL from environment variables let connectionString = env.HYPERDRIVE.connectionString; const sql = postgres(connectionString, { max: 5, // idle_timeout: 30, ssl: import.meta.env.DEV, // Debug options - remove in production debug: true, fetch_types: false, }); return sql; } ``` ### Additional context Unfortunately, it's only reproducible in a live Cloudflare environment.
Author
Owner

@imjlk commented on GitHub (Jun 3, 2025):

Using the Drizzle client and postgres.js seems to work well. Below is the drizzle and better-auth snippet I configured.

Drizzle client:

import { type PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

export function createDrizzleClient(
	connectionString: string,
): PostgresJsDatabase<typeof schema> {
	const client = postgres(connectionString, {
		// Ref:https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/#22-connect-drizzle-orm-to-the-database-with-hyperdrive
		max: 5,
		fetch_types: false,
	});

	return drizzle(client, { schema });
}

export type DrizzleClient = ReturnType<typeof createDrizzleClient>;

better-auth:

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import type { DrizzleClient } from "./server/db";
import * as authSchema from "./server/db/schema/auth";

export const createAuth = (db: DrizzleClient) =>
	betterAuth({
		database: drizzleAdapter(db, {
			provider: "pg",
			schema: {
				...authSchema,
			},
		}),
		// Your plugins or other configurations...
	});

export type BetterAuth = ReturnType<typeof createAuth>;
@imjlk commented on GitHub (Jun 3, 2025): Using the Drizzle client and postgres.js seems to work well. Below is the drizzle and better-auth snippet I configured. Drizzle client: ```ts import { type PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js"; import postgres from "postgres"; import * as schema from "./schema"; export function createDrizzleClient( connectionString: string, ): PostgresJsDatabase<typeof schema> { const client = postgres(connectionString, { // Ref:https://developers.cloudflare.com/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/#22-connect-drizzle-orm-to-the-database-with-hyperdrive max: 5, fetch_types: false, }); return drizzle(client, { schema }); } export type DrizzleClient = ReturnType<typeof createDrizzleClient>; ``` better-auth: ```ts import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import type { DrizzleClient } from "./server/db"; import * as authSchema from "./server/db/schema/auth"; export const createAuth = (db: DrizzleClient) => betterAuth({ database: drizzleAdapter(db, { provider: "pg", schema: { ...authSchema, }, }), // Your plugins or other configurations... }); export type BetterAuth = ReturnType<typeof createAuth>; ```
Author
Owner

@dosubot[bot] commented on GitHub (Sep 2, 2025):

Hi, @stuartwk. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You reported indefinite timeouts using a Cloudflare Hyperdrive Postgres connection string with better-auth on Cloudflare Workers (v1.2.6).
  • The issue only occurred in the live Cloudflare environment; normal Postgres connection strings worked fine.
  • Contributor imjlk suggested using the Drizzle client with postgres.js as a workaround.
  • Example code was provided showing successful integration of Drizzle ORM and better-auth with Hyperdrive.
  • The issue was resolved by switching to the Drizzle client approach.

Next Steps:

  • Please confirm if this workaround is still relevant with the latest version of better-auth.
  • If so, you can keep the discussion open by commenting; otherwise, I will automatically close this issue in 7 days.

Thanks for your understanding and contribution!

@dosubot[bot] commented on GitHub (Sep 2, 2025): Hi, @stuartwk. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported indefinite timeouts using a Cloudflare Hyperdrive Postgres connection string with better-auth on Cloudflare Workers (v1.2.6). - The issue only occurred in the live Cloudflare environment; normal Postgres connection strings worked fine. - Contributor imjlk suggested using the Drizzle client with postgres.js as a workaround. - Example code was provided showing successful integration of Drizzle ORM and better-auth with Hyperdrive. - The issue was resolved by switching to the Drizzle client approach. **Next Steps:** - Please confirm if this workaround is still relevant with the latest version of better-auth. - If so, you can keep the discussion open by commenting; otherwise, I will automatically close this issue in 7 days. Thanks for your understanding and contribution!
Author
Owner

@ymansurozer commented on GitHub (Nov 15, 2025):

Having the exact same issue. Tried using Hyperdrive with Planetscale Postgres and Prisma Postgres but certain Better Auth operations like signing in just stay hanging, while sending separate db queries work fine (so the issue is with Better Auth). The weird thing is there is no error log at all. This is where it gets stuck on my project:

2025-11-15T21:15:33.770Z INFO [Better Auth]: [Prisma Adapter] #1 [1/3] findMany: {
  model: 'verification',
  where: [
    {
      operator: 'eq',
      connector: 'AND',
      field: 'identifier',
      value: 'some value'
    }
  ],
  limit: 1,
  sortBy: { field: 'createdAt', direction: 'desc' },
  offset: undefined
}

Edit: I am using Prisma ORM.

@ymansurozer commented on GitHub (Nov 15, 2025): Having the exact same issue. Tried using Hyperdrive with Planetscale Postgres and Prisma Postgres but certain Better Auth operations like signing in just stay hanging, while sending separate db queries work fine (so the issue is with Better Auth). The weird thing is there is no error log at all. This is where it gets stuck on my project: ``` 2025-11-15T21:15:33.770Z INFO [Better Auth]: [Prisma Adapter] #1 [1/3] findMany: { model: 'verification', where: [ { operator: 'eq', connector: 'AND', field: 'identifier', value: 'some value' } ], limit: 1, sortBy: { field: 'createdAt', direction: 'desc' }, offset: undefined } ``` Edit: I am using Prisma ORM.
Author
Owner

@EverybodyKurts commented on GitHub (Nov 25, 2025):

Hey everybody, I just wanna let y'all know that there is a better auth community plugin for handling hyperdrive. You can find it here: https://github.com/zpg6/better-auth-cloudflare

I hope this helps people :)

@EverybodyKurts commented on GitHub (Nov 25, 2025): Hey everybody, I just wanna let y'all know that there is a better auth community plugin for handling hyperdrive. You can find it here: https://github.com/zpg6/better-auth-cloudflare I hope this helps people :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1042