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 generate
npx @better-auth/cli@latest migrate
Use 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,
set the PostgreSQL user's default schema before generating or migrating:
ALTER USER authuser SET SEARCH_PATH TO auth;
alternatively, append the option to your connection URI, for example:
postgres://<DATABASE_URL>?option=-c search_path=auth
URL-encode if needed: ?option=-c%20search_path%3Dauth
.
Ensure the target schema exists and the database user has the required permissions.
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.