Skip to main content
PostgreSQL is a powerful open-source relational database. Better Auth uses Kysely under the hood for PostgreSQL, which means any database Kysely supports is also supported.

Installation

1

Install the pg driver

2

Configure Better Auth

Pass a pg.Pool instance directly to the database option:
auth.ts

Schema generation and migration

The Better Auth CLI can both generate the schema and apply migrations directly against your PostgreSQL database.

Connection pooling

Use pg.Pool (not pg.Client) so that Better Auth can reuse connections across requests. Configure pool size based on your workload:
auth.ts
For serverless deployments, consider using a connection pooler such as PgBouncer or Neon’s pooled connection string to avoid exhausting database connections.

Using a non-default schema

By default Better Auth creates tables in the public schema. To use a different schema (e.g. auth), you have three options.
auth.ts
URL-encode the parameter if needed: ?options=-c%20search_path%3Dauth.

Option 2: Set search_path in Pool options

auth.ts

Option 3: Set the default schema for the database user

Reconnect after running this command for the change to take effect.

Prerequisites for a non-default schema

Before using a custom schema, make sure it exists and your user has the necessary permissions:
When running npx auth migrate, the CLI automatically detects your configured search_path and creates tables in the correct schema. Tables in other schemas are ignored.

Experimental joins

Enabling joins allows Better Auth to use SQL JOIN clauses to fetch related data in a single query instead of multiple round-trips. Endpoints such as /get-session and /get-full-organization see 2-3x latency improvements with high-latency databases.
auth.ts
You may need to run migrations after enabling joins.