docs/content/docs/adapters/postgresql.mdx
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 about PostgreSQL.
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",
}),
});
The Better Auth CLI allows you to generate or migrate your database schema based on your Better Auth configuration and plugins.
<table> <thead> <tr className="border-b"> <th> <p className="font-bold text-[16px] mb-1">PostgreSQL Schema Generation</p> </th> <th>
<p className="font-bold text-[16px] mb-1">PostgreSQL Schema Migration</p>
</th>
</tr>
<Tabs items={["migrate", "generate"]}>
<Tab value="migrate">
package-install npx auth@latest migrate
</Tab>
Database joins are useful when Better-Auth needs to fetch related data from multiple tables in a single query.
Endpoints like /get-session, /get-full-organization and many others benefit greatly from this feature,
seeing upwards of 2x to 3x performance improvements depending on database latency.
The Kysely PostgreSQL dialect supports joins out of the box since version 1.4.0.
To enable this feature, you need to set the experimental.joins option to true in your auth configuration.
import { betterAuth } from "better-auth";
export const auth = betterAuth({
experimental: { joins: true }
});
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:
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.
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: "my-db",
options: "-c search_path=auth",
}),
});
Set the PostgreSQL user's default schema:
ALTER USER your_user SET search_path TO auth;
After setting this, reconnect to apply the changes.
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;
The Better Auth CLI migration system automatically detects your configured search_path:
npx auth migrate, it inspects only the tables in your configured schemapublic) are ignored, preventing conflictsSolution: This usually means the schema doesn't exist or the user lacks permissions. Create the schema and grant permissions as shown above. </Callout>
<Callout type="info"> **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.
</Callout>
PostgreSQL is supported under the hood via the Kysely adapter, any database supported by Kysely would also be supported. (<Link href="/docs/adapters/other-relational-databases">Read more here</Link>)
If you're looking for performance improvements or tips, take a look at our guide to <Link href="/docs/guides/optimizing-for-performance">performance optimizations</Link>.