Skip to content
pgsodium

pgsodium

pgsodium : Postgres extension for libsodium functions

Overview

ID Extension Package Version Category License Language
7020
pgsodium
pgsodium
3.1.9
SEC
BSD 3-Clause
C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--sLd--
No
Yes
Yes
Yes
no
no
Relationships
Schemas pgsodium
Need By
supabase_vault
See Also
pgsmcrypto
pgcryptokey
pgcrypto
anon
pg_tde
sslutils
faker

+fix missing pg17

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT
PIGSTY
3.1.9
18
17
16
15
14
pgsodium -
RPM
PIGSTY
3.1.9
18
17
16
15
14
pgsodium_$v -
DEB
PIGSTY
3.1.9
18
17
16
15
14
postgresql-$v-pgsodium -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64
PGDG 3.1.9
PGDG 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
el8.aarch64
PGDG 3.1.9
PGDG 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
el9.x86_64
PGDG 3.1.9
PGDG 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
el9.aarch64
PGDG 3.1.9
PGDG 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
el10.x86_64
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
el10.aarch64
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
PGDG 3.1.9
d12.x86_64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
d12.aarch64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
d13.x86_64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
d13.aarch64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u22.x86_64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u22.aarch64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u24.x86_64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u24.aarch64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u26.x86_64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
u26.aarch64
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
PIGSTY 3.1.9
Package Version OS ORG SIZE File URL
pgsodium_18 3.1.9 el8.x86_64 pgdg 71.4 KiB pgsodium_18-3.1.9-4PGDG.rhel8.x86_64.rpm
pgsodium_18 3.1.9 el8.x86_64 pigsty 60.4 KiB pgsodium_18-3.1.9-1PIGSTY.el8.x86_64.rpm
pgsodium_18 3.1.9 el8.aarch64 pgdg 67.9 KiB pgsodium_18-3.1.9-4PGDG.rhel8.aarch64.rpm
pgsodium_18 3.1.9 el8.aarch64 pigsty 57.7 KiB pgsodium_18-3.1.9-1PIGSTY.el8.aarch64.rpm
pgsodium_18 3.1.9 el9.x86_64 pgdg 70.8 KiB pgsodium_18-3.1.9-4PGDG.rhel9.x86_64.rpm
pgsodium_18 3.1.9 el9.x86_64 pigsty 56.9 KiB pgsodium_18-3.1.9-1PIGSTY.el9.x86_64.rpm
pgsodium_18 3.1.9 el9.aarch64 pgdg 67.5 KiB pgsodium_18-3.1.9-4PGDG.rhel9.aarch64.rpm
pgsodium_18 3.1.9 el9.aarch64 pigsty 54.4 KiB pgsodium_18-3.1.9-1PIGSTY.el9.aarch64.rpm
pgsodium_18 3.1.9 el10.x86_64 pgdg 70.8 KiB pgsodium_18-3.1.9-4PGDG.rhel10.x86_64.rpm
pgsodium_18 3.1.9 el10.x86_64 pigsty 56.8 KiB pgsodium_18-3.1.9-1PIGSTY.el10.x86_64.rpm
pgsodium_18 3.1.9 el10.aarch64 pgdg 68.1 KiB pgsodium_18-3.1.9-4PGDG.rhel10.aarch64.rpm
pgsodium_18 3.1.9 el10.aarch64 pigsty 54.1 KiB pgsodium_18-3.1.9-1PIGSTY.el10.aarch64.rpm
postgresql-18-pgsodium 3.1.9 d12.x86_64 pigsty 182.9 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~bookworm_amd64.deb
postgresql-18-pgsodium 3.1.9 d12.aarch64 pigsty 179.3 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~bookworm_arm64.deb
postgresql-18-pgsodium 3.1.9 d13.x86_64 pigsty 183.1 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~trixie_amd64.deb
postgresql-18-pgsodium 3.1.9 d13.aarch64 pigsty 179.4 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~trixie_arm64.deb
postgresql-18-pgsodium 3.1.9 u22.x86_64 pigsty 194.4 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~jammy_amd64.deb
postgresql-18-pgsodium 3.1.9 u22.aarch64 pigsty 190.9 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~jammy_arm64.deb
postgresql-18-pgsodium 3.1.9 u24.x86_64 pigsty 192.5 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~noble_amd64.deb
postgresql-18-pgsodium 3.1.9 u24.aarch64 pigsty 189.5 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~noble_arm64.deb
postgresql-18-pgsodium 3.1.9 u26.x86_64 pigsty 192.4 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~resolute_amd64.deb
postgresql-18-pgsodium 3.1.9 u26.aarch64 pigsty 189.1 KiB postgresql-18-pgsodium_3.1.9-1PIGSTY~resolute_arm64.deb

Source

pig build pkg pgsodium;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install pgsodium;		# install via package name, for the active PG version

pig install pgsodium -v 18;   # install for PG 18
pig install pgsodium -v 17;   # install for PG 17
pig install pgsodium -v 16;   # install for PG 16
pig install pgsodium -v 15;   # install for PG 15
pig install pgsodium -v 14;   # install for PG 14

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'pgsodium';

Create this extension with:

CREATE EXTENSION pgsodium;

Usage

pgsodium: libsodium-based cryptographic functions for PostgreSQL

pgsodium is an encryption library extension for PostgreSQL using the libsodium library. It provides a direct SQL interface to libsodium, server-managed key derivation, and Transparent Column Encryption (TCE).

CREATE EXTENSION pgsodium;

Generating Random Data

SELECT pgsodium.randombytes_random();
SELECT pgsodium.randombytes_buf(16);         -- 16 random bytes
SELECT pgsodium.randombytes_uniform(100);    -- random int 0-99

Secret Key Encryption (Authenticated)

SELECT * FROM pgsodium.crypto_secretbox_keygen();
SELECT pgsodium.crypto_secretbox('message', nonce, key);
SELECT pgsodium.crypto_secretbox_open(ciphertext, nonce, key);

Public Key Encryption

SELECT * FROM pgsodium.crypto_box_new_keypair();
SELECT pgsodium.crypto_box('message', nonce, public_key, secret_key);
SELECT pgsodium.crypto_box_open(ciphertext, nonce, public_key, secret_key);

Public Key Signatures

SELECT * FROM pgsodium.crypto_sign_new_keypair();
SELECT pgsodium.crypto_sign('message', secret_key);
SELECT pgsodium.crypto_sign_open(signed_message, public_key);

Password Hashing

SELECT pgsodium.crypto_pwhash_str('my_password');
SELECT pgsodium.crypto_pwhash_str_verify(hash, 'my_password');

Hashing

SELECT pgsodium.crypto_generichash('data');
SELECT pgsodium.crypto_shorthash('data', key);

Server Key Management

pgsodium can load an external root key into memory that is never accessible to SQL. Sub-keys are derived by key id:

SELECT * FROM pgsodium.create_key();
-- Returns a UUID key id for use with TCE or encryption functions

Transparent Column Encryption (TCE)

CREATE TABLE private.users (
    id bigserial PRIMARY KEY,
    secret text
);

SECURITY LABEL FOR pgsodium ON COLUMN private.users.secret
  IS 'ENCRYPT WITH KEY ID dfc44293-fa78-4a1a-9ef9-7e600e63e101';

Encrypted data is stored on disk and automatically decrypted via a generated view.

Security Roles

  • pgsodium_keyiduser – less privileged, can only access keys by UUID
  • pgsodium_keymaker – more privileged, can work with raw keys
Last updated on