anon

pg_anon : PostgreSQL Anonymizer (anon) extension

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
7050
anon
pg_anon
3.0.1
SEC
PostgreSQL
Rust
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--sLd--
No
Yes
Yes
Yes
no
no
Relationships
Schemasanon
See Also
faker
pgsodium
pgcrypto
pgaudit
set_user
pg_tde

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXT
PIGSTY
3.0.1
18
17
16
15
14
pg_anon-
RPM
PIGSTY
3.0.1
18
17
16
15
14
pg_anon_$v-
DEB
PIGSTY
3.0.1
18
17
16
15
14
postgresql-$v-pg-anon-
Linux / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
el8.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
el9.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
el9.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
el10.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
el10.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
d12.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
d12.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
d13.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
d13.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
u22.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
u22.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
u24.x86_64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
u24.aarch64
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1
PIGSTY 3.0.1

Source

pig build pkg pg_anon;		# 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 pg_anon;		# install via package name, for the active PG version
pig install anon;		# install by extension name, for the current active PG version

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

Config this extension to shared_preload_libraries:

shared_preload_libraries = 'anon';

Create this extension with:

CREATE EXTENSION anon;

Usage

anon: Anonymization & Data Masking for PostgreSQL

postgresql_anonymizer (extension name: anon) masks or replaces personally identifiable information (PII) using a declarative approach. Masking rules are defined directly in the database schema using security labels.

CREATE EXTENSION IF NOT EXISTS anon CASCADE;
SELECT anon.init();

Declaring Masking Rules

SECURITY LABEL FOR anon ON COLUMN player.name
  IS 'MASKED WITH FUNCTION anon.fake_last_name()';

SECURITY LABEL FOR anon ON COLUMN player.id
  IS 'MASKED WITH VALUE NULL';

Static Masking

Permanently replace PII in the database:

SECURITY LABEL FOR anon ON COLUMN customer.full_name
  IS 'MASKED WITH FUNCTION anon.fake_first_name() || '' '' || anon.fake_last_name()';

SECURITY LABEL FOR anon ON COLUMN customer.birth
  IS 'MASKED WITH FUNCTION anon.random_date_between(''1920-01-01''::DATE, now())';

SELECT anon.anonymize_database();
-- Also available: anon.anonymize_table(), anon.anonymize_column()

Dynamic Masking

Hide PII from specific roles while others see original data:

SELECT anon.start_dynamic_masking();

CREATE ROLE skynet LOGIN;
SECURITY LABEL FOR anon ON ROLE skynet IS 'MASKED';

SECURITY LABEL FOR anon ON COLUMN people.lastname
  IS 'MASKED WITH FUNCTION anon.fake_last_name()';

SECURITY LABEL FOR anon ON COLUMN people.phone
  IS 'MASKED WITH FUNCTION anon.partial(phone, 2, $$******$$, 2)';

When skynet queries the table, masked data is returned automatically.

Anonymous Dumps

pg_dump_anon.sh -h localhost -p 5432 -U bob bob_db > dump.sql

Common Masking Functions

FunctionDescription
anon.fake_first_name()Random first name
anon.fake_last_name()Random last name
anon.fake_company()Random company name
anon.random_date_between(d1, d2)Random date in range
anon.random_zip()Random zip code
anon.partial(value, prefix, padding, suffix)Partial scrambling
anon.random_string(n)Random string of length n
anon.random_int_between(i1, i2)Random integer in range
Last updated on