Skip to content
pgsodium

pgsodium

pgsodium : Postgres extension for libsodium functions

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
7020
pgsodium
pgsodium
3.1.9
SEC
BSD 3-Clause
C
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--
No
Yes
Yes
Yes
no
no
Relationships
Schemaspgsodium
Need By
supabase_vault
See Also
pgsmcrypto
pgcryptokey
pgcrypto
anon
pg_tde
sslutils
faker

+fix missing pg17

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
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 / PGPG18PG17PG16PG15PG14
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
PackageVersionOSORGSIZEFile URL
pgsodium_183.1.9el8.x86_64pgdg71.4 KiBpgsodium_18-3.1.9-4PGDG.rhel8.x86_64.rpm
pgsodium_183.1.9el8.x86_64pigsty60.4 KiBpgsodium_18-3.1.9-1PIGSTY.el8.x86_64.rpm
pgsodium_183.1.9el8.aarch64pgdg67.9 KiBpgsodium_18-3.1.9-4PGDG.rhel8.aarch64.rpm
pgsodium_183.1.9el8.aarch64pigsty57.7 KiBpgsodium_18-3.1.9-1PIGSTY.el8.aarch64.rpm
pgsodium_183.1.9el9.x86_64pgdg70.8 KiBpgsodium_18-3.1.9-4PGDG.rhel9.x86_64.rpm
pgsodium_183.1.9el9.x86_64pigsty56.9 KiBpgsodium_18-3.1.9-1PIGSTY.el9.x86_64.rpm
pgsodium_183.1.9el9.aarch64pgdg67.5 KiBpgsodium_18-3.1.9-4PGDG.rhel9.aarch64.rpm
pgsodium_183.1.9el9.aarch64pigsty54.4 KiBpgsodium_18-3.1.9-1PIGSTY.el9.aarch64.rpm
pgsodium_183.1.9el10.x86_64pgdg70.8 KiBpgsodium_18-3.1.9-4PGDG.rhel10.x86_64.rpm
pgsodium_183.1.9el10.x86_64pigsty56.8 KiBpgsodium_18-3.1.9-1PIGSTY.el10.x86_64.rpm
pgsodium_183.1.9el10.aarch64pgdg68.1 KiBpgsodium_18-3.1.9-4PGDG.rhel10.aarch64.rpm
pgsodium_183.1.9el10.aarch64pigsty54.1 KiBpgsodium_18-3.1.9-1PIGSTY.el10.aarch64.rpm
postgresql-18-pgsodium3.1.9d12.x86_64pigsty182.9 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~bookworm_amd64.deb
postgresql-18-pgsodium3.1.9d12.aarch64pigsty179.3 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~bookworm_arm64.deb
postgresql-18-pgsodium3.1.9d13.x86_64pigsty183.1 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~trixie_amd64.deb
postgresql-18-pgsodium3.1.9d13.aarch64pigsty179.4 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~trixie_arm64.deb
postgresql-18-pgsodium3.1.9u22.x86_64pigsty194.4 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~jammy_amd64.deb
postgresql-18-pgsodium3.1.9u22.aarch64pigsty190.9 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~jammy_arm64.deb
postgresql-18-pgsodium3.1.9u24.x86_64pigsty192.5 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~noble_amd64.deb
postgresql-18-pgsodium3.1.9u24.aarch64pigsty189.5 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~noble_arm64.deb
postgresql-18-pgsodium3.1.9u26.x86_64pigsty192.4 KiBpostgresql-18-pgsodium_3.1.9-1PIGSTY~resolute_amd64.deb
postgresql-18-pgsodium3.1.9u26.aarch64pigsty189.1 KiBpostgresql-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