---
title: "Use PostgreSQL Anonymizer to Mask Data"
description: "Mask personal data on Managed PostgreSQL with PostgreSQL Anonymizer: static masking for copies, dynamic masking for selected roles."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/use-postgresql-anonymizer/
product: "PostgreSQL"
section: product
last_updated: 2026-11-10
---
> For AI agents: the documentation index is at https://community.exoscale.com/llms.txt. Every page is available as markdown at `<page URL>index.md` or with `Accept: text/markdown`.

# 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 `psql` session as `avnadmin`. See
  [Connect with psql](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/connect-with-psql/index.md).
- The SQL name of the extension is `anon`. Check the version offered on your service:

  ```sql
  SELECT name, default_version
  FROM pg_available_extensions
  WHERE name = 'anon';
  ```

## Enable the Extension

As `avnadmin`, in the database to mask:

```sql
CREATE EXTENSION anon;
```

```sql
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:

```sql
SELECT anon.init();
```

```sql
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.

```sql
CREATE TABLE public.customers (
    id         int PRIMARY KEY,
    last_name  text,
    email      text,
    phone      text,
    birth_date date,
    balance    numeric
);
```

```sql
SECURITY LABEL FOR anon ON COLUMN public.customers.last_name
IS 'MASKED WITH FUNCTION anon.hash(last_name)';
```

```sql
SECURITY LABEL FOR anon ON COLUMN public.customers.email
IS 'MASKED WITH FUNCTION anon.partial_email(email)';
```

```sql
SECURITY LABEL FOR anon ON COLUMN public.customers.phone
IS 'MASKED WITH VALUE NULL';
```

```sql
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:

```sql
SELECT relnamespace, relname, attname, masking_function, masking_value
FROM anon.pg_masking_rules
ORDER BY relname, attname;
```

Remove one rule, or all of them:

```sql
SECURITY LABEL FOR anon ON COLUMN public.customers.phone IS NULL;
```

```sql
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:

```sql
SELECT * FROM anon.detect('en_US');
```

## Static Masking

Static masking overwrites the data with the result of the rules:

```sql
SELECT anon.anonymize_column('public.customers', 'email');
```

```sql
SELECT anon.anonymize_table('public.customers');
```

```sql
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](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/fork-a-service/index.md)), 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](https://postgresql-anonymizer.readthedocs.io/en/stable/)
for the value a column without a rule receives. Turn it on for a database:

```sql
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.

1. Declare the rules on the columns, as above.

2. Label the roles that must see masked data:

   ```sql
   SECURITY LABEL FOR anon ON ROLE USERNAME IS 'MASKED';
   ```

   Parameters:

   - `USERNAME`: an existing role, for example a service user created with
     `exo dbaas user create SERVICE_NAME USERNAME -z ZONE`. It also needs `SELECT` on the
     tables.

3. Turn on dynamic masking for the database:

   ```sql
   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:

```sql
SELECT email, phone FROM public.customers;
```

```text
         email         | phone
-----------------------+-------
 ja******@ex******.com |
```

`avnadmin` still sees `jane.dupont@example.com`.

List the labeled roles:

```sql
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:

```sql
SECURITY LABEL FOR anon ON ROLE USERNAME IS NULL;
```

```sql
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`.

```sql
SET anon.salt = 'A_LONG_RANDOM_SECRET';
```

```sql
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:

```sql
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.

## Related Pages

- [Manage extensions](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-extensions/index.md)
- [Fork a service](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/fork-a-service/index.md)
- [Create read-only users](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/create-read-only-users/index.md)
- [PostgreSQL Anonymizer documentation](https://postgresql-anonymizer.readthedocs.io/en/stable/) ↗

