pg_session_jwt

Manage authentication sessions using JWTs

Overview

PackageVersionCategoryLicenseLanguage
pg_session_jwt0.4.0SECApache-2.0Rust
IDExtensionBinLibLoadCreateTrustRelocSchema
7040pg_session_jwtNoYesNoYesYesNoauth
Relatedpgjwt pgaudit pgsodium supabase_vault anon

manual updated pgrx by Vonng

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.4.01817161514pg_session_jwt-
RPMPIGSTY0.4.01817161514pg_session_jwt_$v-
DEBPIGSTY0.4.01817161514postgresql-$v-pg-session-jwt-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
d13.x86_64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
d13.aarch64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
u22.x86_64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
u22.aarch64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
u24.x86_64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
u24.aarch64
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0
PIGSTY 0.4.0

Build

You can build the RPM / DEB packages for pg_session_jwt using pig build:

pig build pkg pg_session_jwt         # build RPM / DEB packages

Install

You can install pg_session_jwt directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_session_jwt;          # Install for current active PG version
pig ext install -y pg_session_jwt -v 18  # PG 18
pig ext install -y pg_session_jwt -v 17  # PG 17
pig ext install -y pg_session_jwt -v 16  # PG 16
pig ext install -y pg_session_jwt -v 15  # PG 15
pig ext install -y pg_session_jwt -v 14  # PG 14
dnf install -y pg_session_jwt_18       # PG 18
dnf install -y pg_session_jwt_17       # PG 17
dnf install -y pg_session_jwt_16       # PG 16
dnf install -y pg_session_jwt_15       # PG 15
dnf install -y pg_session_jwt_14       # PG 14
apt install -y postgresql-18-pg-session-jwt   # PG 18
apt install -y postgresql-17-pg-session-jwt   # PG 17
apt install -y postgresql-16-pg-session-jwt   # PG 16
apt install -y postgresql-15-pg-session-jwt   # PG 15
apt install -y postgresql-14-pg-session-jwt   # PG 14

Create Extension:

CREATE EXTENSION pg_session_jwt;

Usage

pg_session_jwt: JWT session management for PostgreSQL

pg_session_jwt handles authenticated sessions through JWTs. When configured with a JWK, it verifies JWT authenticity. Without a JWK, it falls back to PostgREST-compatible request.jwt.claims.

CREATE EXTENSION pg_session_jwt;

Mode 1: JWK Validation

Set the JWK at connection time via libpq options:

export PGOPTIONS="-c pg_session_jwt.jwk=$MY_JWK"

Then within the session:

SELECT auth.init();                        -- Initialize with JWK
SELECT auth.jwt_session_init('eyJ...');    -- Set and validate the JWT
SELECT auth.user_id();                     -- Get the 'sub' claim
SELECT auth.session();                     -- Get full JWT payload as JSONB

Mode 2: PostgREST-Compatible (No JWK)

Works out of the box with PostgREST. No initialization needed:

SELECT auth.user_id();   -- Returns 'sub' from request.jwt.claims
SELECT auth.session();   -- Returns full claims as JSONB

Functions

FunctionReturnsDescription
auth.init()voidInitialize session using JWK
auth.jwt_session_init(jwt text)voidSet and validate a JWT
auth.session()jsonbGet JWT payload or fallback claims
auth.jwt()jsonbAlias for auth.session()
auth.user_id()textGet the sub claim
auth.uid()uuidGet sub as UUID (or NULL)

Configuration

ParameterDescription
pg_session_jwt.jwkJWK for JWT validation (set at startup or connection)
pg_session_jwt.audit_logEnable audit logging (on/off)

RLS Example

CREATE POLICY user_isolation ON my_table
    USING (user_id = auth.user_id());

Last Modified 2026-03-12: add pg extension catalog (95749bf)