pgjwt
JSON Web Token API for Postgresql
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pgjwt | 0.2.0 | UTIL | MIT | SQL |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4160 | pgjwt | No | Yes | No | Yes | Yes | No | - |
| Related | pgcrypto http pg_net pg_curl pgjq sparql pgcrypto gzip bzip |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PIGSTY | 0.2.0 | 1817161514 | pgjwt | pgcrypto |
| RPM | PIGSTY | 0.2.0 | 1817161514 | pgjwt_$v | - |
| DEB | PIGSTY | 0.2.0 | 1817161514 | postgresql-$v-pgjwt | - |
Build
You can build the RPM / DEB packages for pgjwt using pig build:
pig build pkg pgjwt # build RPM / DEB packages
Install
You can install pgjwt 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 pgjwt; # Install for current active PG version
pig ext install -y pgjwt -v 18 # PG 18
pig ext install -y pgjwt -v 17 # PG 17
pig ext install -y pgjwt -v 16 # PG 16
pig ext install -y pgjwt -v 15 # PG 15
pig ext install -y pgjwt -v 14 # PG 14
dnf install -y pgjwt_18 # PG 18
dnf install -y pgjwt_17 # PG 17
dnf install -y pgjwt_16 # PG 16
dnf install -y pgjwt_15 # PG 15
dnf install -y pgjwt_14 # PG 14
apt install -y postgresql-18-pgjwt # PG 18
apt install -y postgresql-17-pgjwt # PG 17
apt install -y postgresql-16-pgjwt # PG 16
apt install -y postgresql-15-pgjwt # PG 15
apt install -y postgresql-14-pgjwt # PG 14
Create Extension:
CREATE EXTENSION pgjwt CASCADE; -- requires: pgcrypto
Usage
Requires the pgcrypto extension.
Sign a Token
SELECT sign(
'{"sub":"1234567890","name":"John Doe","admin":true}',
'secret'
);
Returns a JWT string like: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOi...
Verify a Token
SELECT * FROM verify(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ',
'secret'
);
Returns:
| header | payload | valid |
|---|---|---|
{"alg":"HS256","typ":"JWT"} | {"sub":"1234567890","name":"John Doe","admin":true} | t |
Algorithm Selection
sign() and verify() accept an optional third argument for the algorithm: 'HS256' (default), 'HS384', or 'HS512'.
SELECT sign('{"data":"value"}', 'secret', 'HS384');
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.