PostgreSQL
PostgreSQL is a powerful, open-source relational database management system known for its advanced features, extensibility, and support for complex queries and large datasets. Read more here.
Example Usage
Make sure you have PostgreSQL installed and configured. Then, you can connect it straight into Better Auth.
import { betterAuth } from "better-auth";
import { Pool } from "pg";
export const auth = betterAuth({
database: new Pool({
connectionString: "postgres://user:password@localhost:5432/database",
}),
});For more information, read Kysely's documentation to the PostgresDialect.
Schema generation & migration
The Better Auth CLI allows you to generate or migrate your database schema based on your Better Auth configuration and plugins.
PostgreSQL Schema Generation | PostgreSQL Schema Migration |
|---|---|
| ✅ Supported | ✅ Supported |
npx @better-auth/cli@latest generatenpx @better-auth/cli@latest migrateUse a non-default schema
In most cases, the default schema is public. To have Better Auth use a
non-default schema (e.g., auth) for its tables, you have several options:
Option 1: Set search_path in connection string (Recommended)
Append the options parameter to your connection URI:
import { betterAuth } from "better-auth";
import { Pool } from "pg";
export const auth = betterAuth({
database: new Pool({
connectionString: "postgres://user:password@localhost:5432/database?options=-c search_path=auth",
}),
});URL-encode if needed: ?options=-c%20search_path%3Dauth.
Option 2: Set search_path using Pool options
import { betterAuth } from "better-auth";
import { Pool } from "pg";
export const auth = betterAuth({
database: new Pool({
host: "localhost",
port: 5432,
user: "postgres",
password: "password",
database: "mydb",
options: "-c search_path=auth",
}),
});Option 3: Set default schema for database user
Set the PostgreSQL user's default schema:
ALTER USER your_user SET search_path TO auth;After setting this, reconnect to apply the changes.
Prerequisites
Before using a non-default schema, ensure:
-
The schema exists:
CREATE SCHEMA IF NOT EXISTS auth; -
The user has appropriate permissions:
GRANT ALL PRIVILEGES ON SCHEMA auth TO your_user; GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO your_user; ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT ALL ON TABLES TO your_user;
How it works
The Better Auth CLI migration system automatically detects your configured search_path:
- When running
npx @better-auth/cli migrate, it inspects only the tables in your configured schema - Tables in other schemas (e.g.,
public) are ignored, preventing conflicts - All new tables are created in your specified schema
Troubleshooting
Issue: "relation does not exist" error during migration
Solution: This usually means the schema doesn't exist or the user lacks permissions. Create the schema and grant permissions as shown above.
Verifying your schema configuration:
You can verify which schema Better Auth will use by checking the search_path:
SHOW search_path;This should return your custom schema (e.g., auth) as the first value.
Additional Information
PostgreSQL is supported under the hood via the Kysely adapter, any database supported by Kysely would also be supported. (Read more here)
If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.