pg adapter

Adapter for pg provided by the PostgreSQL adapter package. This adapter can be used for @vercel/postgres and @neondatabase/serverless as well. See guide Using @vercel/postgres.

import { pg } from "@lucia-auth/adapter-postgresql";
const pg: (
	pool: Pool,
	tableNames: {
		user: string;
		key: string;
		session: string | null;
	}
) => InitializeAdapter<Adapter>;
Parameters#

Table names are automatically escaped.

nametypedescription
poolPoolpg connection pool
tableNames.userstringUser table name
tableNames.keystringKey table name
tableNames.sessionstring | nullSession table name - can be null when using alongside a session adapter

Installation#

npm i @lucia-auth/adapter-postgresql
pnpm add @lucia-auth/adapter-postgresql
yarn add @lucia-auth/adapter-postgresql

Usage#

import { lucia } from "lucia";
import { pg } from "@lucia-auth/adapter-postgresql";
import postgres from "pg";

const pool = new postgres.Pool({
	connectionString: CONNECTION_URL
});

const auth = lucia({
	adapter: pg(pool, {
		user: "auth_user",
		key: "user_key",
		session: "user_session"
	})
	// ...
});

PostgreSQL schema#

You can choose any table names, just make sure to define them in the adapter argument. The id columns are not UUID types with the default configuration.

User table#

You can add additional columns to store user attributes.

CREATE TABLE auth_user (
    id TEXT NOT NULL PRIMARY KEY
);

Key table#

Make sure to update the foreign key statement if you change the user table name.

CREATE TABLE user_key (
    id TEXT NOT NULL PRIMARY KEY,
    user_id TEXT NOT NULL REFERENCES auth_user(id),
    hashed_password TEXT
);

Session table#

You can add additional columns to store session attributes. Make sure to update the foreign key statement if you change the user table name.

CREATE TABLE user_session (
    id TEXT NOT NULL PRIMARY KEY,
    user_id TEXT NOT NULL REFERENCES auth_user(id),
    active_expires BIGINT NOT NULL,
    idle_expires BIGINT NOT NULL
);