Installation
1
Install the pg driver
- npm
- pnpm
- yarn
- bun
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.- migrate
- generate
Connection pooling
Usepg.Pool (not pg.Client) so that Better Auth can reuse connections across requests. Configure pool size based on your workload:
auth.ts
Using a non-default schema
By default Better Auth creates tables in thepublic schema. To use a different schema (e.g. auth), you have three options.
Option 1: Set search_path in the connection string (recommended)
auth.ts
?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
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 SQLJOIN 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