PostgreSQL-backed authentication
Persist Auth.js users, accounts, and sessions in a PostgreSQL database.
Auth.js · Const
PostgresAdapter is Auth.js’s official PostgreSQL adapter. Give it a PostgreSQL Pool to obtain an Adapter for the NextAuth configuration.
PostgresAdapter accepts a PostgreSQL connection pool and produces an Auth.js-compatible adapter. Supply the resulting adapter to NextAuth so Auth.js can use PostgreSQL for persisted authentication data.
function default(client): Adapter| Name | Type | Description |
|---|---|---|
client | Pool | PostgreSQL connection pool used by the adapter. |
Adapter
import NextAuth from "next-auth"
import PostgresAdapter from "@auth/pg-adapter"
import { Pool } from "pg"
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
})
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PostgresAdapter(pool),
providers: [],
})Create a PostgreSQL connection pool and pass it to PostgresAdapter in the Auth.js configuration.
Persist Auth.js users, accounts, and sessions in a PostgreSQL database.
Connect a NextAuth configuration to an existing node-postgres pool.
Run Auth.js with Neon-compatible PostgreSQL in an edge environment.
PostgresAdapter is the default export from @auth/pg-adapter. It takes a pg Pool and returns an Adapter that can be passed as the adapter option to NextAuth.
Follow-up: What must the caller provide? A PostgreSQL Pool configured for the target database; the adapter does not accept a database name or connection string directly in this signature.
Install @auth/pg-adapter together with pg, create a Pool, and invoke PostgresAdapter(pool). The returned value is then assigned to adapter in the Auth.js configuration.
Follow-up: How would an edge deployment differ? Use @neondatabase/serverless for Neon-compatible PostgreSQL instead of the conventional pg package.
The function parameter is named client and has type Pool. Its return type is Adapter, which is the interface Auth.js uses for database persistence.
Follow-up: Why is the return type significant? It means the adapter exposes the standard Auth.js adapter contract rather than a PostgreSQL-specific API.
AdapterPool@auth/pg-adapter@neondatabase/serverless