Use PostgreSQL Anonymizer to Mask Data
Declare masking rules on your columns with PostgreSQL Anonymizer, then either rewrite the data in a copy of the database or hide the real values from selected roles at query time.
Before You Start
Open a
psqlsession asavnadmin. See Connect with psql.The SQL name of the extension is
anon. Check the version offered on your service:SELECT name, default_version FROM pg_available_extensions WHERE name = 'anon';
Enable the Extension
As avnadmin, in the database to mask:
CREATE EXTENSION anon;SELECT anon.version();The user that creates the extension owns it. The masking functions, the fake-data tables and the
anon.pg_masking_rules view are in the anon schema.
Load the Fake-Data Dictionaries
The fake_*() functions need dictionaries that are not loaded at creation:
SELECT anon.init();SELECT anon.is_initialized();Before anon.init(), a fake_*() call returns NULL with
NOTICE: The anon extension is not initialized. The other masking functions, such as
anon.partial(), anon.hash(), anon.noise(), anon.random_*() and anon.pseudo_*(), work
without it.
Declare Masking Rules
A masking rule is a security label on a column. Declaring it changes no data; it only says how the column is masked when masking applies.
CREATE TABLE public.customers (
id int PRIMARY KEY,
last_name text,
email text,
phone text,
birth_date date,
balance numeric
);SECURITY LABEL FOR anon ON COLUMN public.customers.last_name
IS 'MASKED WITH FUNCTION anon.hash(last_name)';SECURITY LABEL FOR anon ON COLUMN public.customers.email
IS 'MASKED WITH FUNCTION anon.partial_email(email)';SECURITY LABEL FOR anon ON COLUMN public.customers.phone
IS 'MASKED WITH VALUE NULL';SECURITY LABEL FOR anon ON COLUMN public.customers.balance
IS 'MASKED WITH FUNCTION anon.noise(balance, 0.15)';MASKED WITH FUNCTION computes the replacement, usually from the original value.
MASKED WITH VALUE puts a constant, NULL included. The result must fit the column type.
List the rules of the database:
SELECT relnamespace, relname, attname, masking_function, masking_value
FROM anon.pg_masking_rules
ORDER BY relname, attname;Remove one rule, or all of them:
SECURITY LABEL FOR anon ON COLUMN public.customers.phone IS NULL;SELECT anon.remove_masks_for_all_columns();Run anon.remove_masks_for_all_columns() with anon.privacy_by_default off. When it is on, the
function also tries index columns and stops with
ERROR: cannot set security label on relation "customers_pkey".
Common Masking Functions
| Function | Result |
|---|---|
anon.partial(value, prefix, padding, suffix) | Keeps the first and last characters. anon.partial('0123456789', 2, '***', 2) returns 01***89. |
anon.partial_email(value) | jane.dupont@example.com becomes ja******@ex******.com. |
anon.hash(value) | Hash of the value, salted with anon.salt. The same input gives the same output. |
anon.digest(value, salt, algorithm) | Hash with an explicit salt and algorithm, for example 'sha256'. |
anon.noise(value, ratio) | Shifts a number or an interval by a random fraction of its value. |
anon.random_string(length), anon.random_int_between(a, b), anon.random_date_between(a, b) | Random values. |
anon.pseudo_first_name(seed), anon.pseudo_last_name(seed), anon.pseudo_email(seed) | Stable pseudonyms. A literal seed needs a type, for example 'x'::text; otherwise the call fails with could not determine polymorphic type. |
anon.fake_first_name(), anon.fake_last_name(), anon.fake_email(), anon.fake_city(), anon.fake_company(), anon.fake_iban() | Realistic values from the dictionaries. Need anon.init(). |
anon.generalize_int4range(value, step), anon.generalize_daterange(value, unit) | The range that contains the value, for example [40,50) or [2020-05-01,2020-06-01). |
Use anon.hash() or a pseudo_*() function when the masked value must be the same in every
table, for example for a customer ID used in joins.
List the columns that look like personal data:
SELECT * FROM anon.detect('en_US');Static Masking
Static masking overwrites the data with the result of the rules:
SELECT anon.anonymize_column('public.customers', 'email');SELECT anon.anonymize_table('public.customers');SELECT anon.anonymize_database();Caution
Static masking destroys the original values, and there is no in-place restore. Run it on a
copy: create a new service from a point in time (see
Fork a service), declare
the rules there, run anon.anonymize_database(), and export the result for development or
testing.
Columns without a rule are left as they are. With privacy by default, every column is treated as
masked, and anon.pg_masking_rules lists all of them. See the
PostgreSQL Anonymizer documentation
for the value a column without a rule receives. Turn it on for a database:
ALTER DATABASE DATABASE_NAME SET anon.privacy_by_default = on;Parameters:
DATABASE_NAME: the database to mask.
Dynamic Masking
Dynamic masking keeps the data and applies the rules at query time, for the roles you label. Other roles see the real values.
Declare the rules on the columns, as above.
Label the roles that must see masked data:
SECURITY LABEL FOR anon ON ROLE USERNAME IS 'MASKED';Parameters:
USERNAME: an existing role, for example a service user created withexo dbaas user create SERVICE_NAME USERNAME -z ZONE. It also needsSELECTon the tables.
Turn on dynamic masking for the database:
ALTER DATABASE DATABASE_NAME SET anon.transparent_dynamic_masking = on;The setting applies to new connections.
The labeled role then reads masked values from the table itself:
SELECT email, phone FROM public.customers; email | phone
-----------------------+-------
ja******@ex******.com |avnadmin still sees jane.dupont@example.com.
List the labeled roles:
SELECT r.rolname, s.label
FROM pg_shseclabel s
JOIN pg_roles r ON r.oid = s.objoid
WHERE s.provider = 'anon';Remove a role from masking, or turn the feature off:
SECURITY LABEL FOR anon ON ROLE USERNAME IS NULL;ALTER DATABASE DATABASE_NAME RESET anon.transparent_dynamic_masking;Warning
Dynamic masking hides values in query results. A role that can filter, join or aggregate on a
masked column may still infer values. Limit the tables and columns the role can reach with
GRANT and REVOKE.
Configure the Extension
The anon.* settings, such as anon.salt, anon.privacy_by_default and
anon.transparent_dynamic_masking, can be set by avnadmin, for the session or for a database.
Other roles get permission denied to set parameter.
SET anon.salt = 'A_LONG_RANDOM_SECRET';ALTER DATABASE DATABASE_NAME SET anon.salt = 'A_LONG_RANDOM_SECRET';Parameters:
A_LONG_RANDOM_SECRET: a value you keep private. With it, the hashes of a small set of values, such as phone numbers, can be computed again and matched.
SHOW anon.salt fails with permission denied to examine "anon.salt". Read the value with the
extension’s function:
SELECT anon.current_setting('anon.salt');Changing anon.salt changes every hash. Set it before producing masked data, and keep it stable
for as long as masked datasets must match each other.