# Masking for Specific Columns

> Source: https://docs.synthesized.io/tdk/latest/user_guide/020_guides/masking_data/masking
> For the complete documentation index, see [llms.txt](https://docs.synthesized.io/llms.txt).

If we run the platform with an empty configuration file, that would be the equivalent of using:

```yaml
default_config:
  mode: KEEP
```

In `KEEP` mode, the platform copies data without any modification.

When processing production data, we need to ensure that no sensitive information leaves the environment. We can achieve this by using `MASKING` mode. Let’s start with a simple configuration:

```yaml
default_config:
  mode: MASKING
safety_mode: RELAXED
```

We can add `TRUNCATE` and `CREATE_IF_NOT_EXIST` options to truncate tables in the output database and create a schema only if it does not exist:

```yaml
default_config:
  mode: MASKING

table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
safety_mode: RELAXED
```

For more schema management options, refer to the [Configuration Reference](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/configuration/configuration_reference).

In this example, we have tables `customer` and `address` linked by the `address_id` field:

![Diagram](https://kroki.io/plantuml/svg/eNpVjkEOAjEIRfc9RS8wF3DhVRoCmCFppwZwYYx3l7F21BXvB3j5qxBnFMXKKWEFswxEyjEfKS8fLkLfEERiroIeiOL3sb52c6gFO_Ge1r5xek4p3sx7Y31bZxh_wcoDL6LmZYO2G-LvYG4g9b9PqEM-tcs5H8VP-efsBX2vSyk=)

Here are samples of data from these tables in the source database:

Table 1. `address` table before masking      
| address\_id | address | district | city\_id | postal\_code | phone |
| --- | --- | --- | --- | --- | --- |
| 
5

 | 

1913 Hanoi Way

 | 

Nagasaki

 | 

463

 | 

35200

 | 

28303384290

 |
| 

6

 | 

1121 Loja Avenue

 | 

California

 | 

449

 | 

17886

 | 

838635286649

 |
| 

7

 | 

692 Joliet Street

 | 

Attika

 | 

38

 | 

83579

 | 

448477190408

 |
| 

8

 | 

1566 Inegl Manor

 | 

Mandalay

 | 

349

 | 

53561

 | 

705814003527

 |

<table class="tableblock frame-none grid-rows stretch"><caption class="title">Table 2. <code>customer</code> table before masking</caption> <colgroup><col style="width: 16.6666%;"> <col style="width: 16.6666%;"> <col style="width: 16.6666%;"> <col style="width: 16.6666%;"> <col style="width: 16.6666%;"> <col style="width: 16.667%;"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><p class="tableblock">customer_id</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">store_id</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">first_name</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">last_name</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">email</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">address_id</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">MARY</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">SMITH</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><a href="mailto:MARY.SMITH@sakilacustomer.org">MARY.SMITH@sakilacustomer.org</a></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">5</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">PATRICIA</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">JOHNSON</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><a href="mailto:PATRICIA.JOHNSON@sakilacustomer.org">PATRICIA.JOHNSON@sakilacustomer.org</a></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">6</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">3</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">1</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">LINDA</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">WILLIAMS</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><a href="mailto:LINDA.WILLIAMS@sakilacustomer.org">LINDA.WILLIAMS@sakilacustomer.org</a></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">7</p></td></tr><tr><td class="tableblock halign-left valign-top"><p class="tableblock">4</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">2</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">BARBARA</p></td><td class="tableblock halign-left valign-top"><p class="tableblock">JONES</p></td><td class="tableblock halign-left valign-top"><p class="tableblock"><a href="mailto:BARBARA.JONES@sakilacustomer.org">BARBARA.JONES@sakilacustomer.org</a></p></td><td class="tableblock halign-left valign-top"><p class="tableblock">8</p></td></tr></tbody></table>

To run the masking transformation, use the following command:

```shell
tdk \
    --input-url jdbc:postgresql://localhost:6000/postgres --input-username postgres --input-password postgres \
    --output-url jdbc:postgresql://localhost:6001/postgres --output-username postgres --output-password postgres \
    --config-file config_masking.tdk.yaml
```

The platform chooses the best masking mechanism based on the data type. You can see what transformations were applied by using the `dry-run` command. For more arguments, refer to the [CLI reference](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/api_reference/command_line_interface)).

Here is the result of default masking applied to `address` table:

Table 3. `address` table after masking      
| address\_id | address | district | city\_id | postal\_code | phone |
| --- | --- | --- | --- | --- | --- |
| 
15183026

 | 

5314 Laypz Acm

 | 

Plxwuyle

 | 

1693424342

 | 

79518

 | 

18846059374

 |
| 

695573888

 | 

0383 Wmwq Cckxcg

 | 

Iyzmcmbynx

 | 

2076144198

 | 

76495

 | 

339497461347

 |
| 

716836103

 | 

306 Kqeywo Hqeekv

 | 

Qfogmd

 | 

1505222168

 | 

13429

 | 

277221410948

 |
| 

1546062460

 | 

3754 Bfgub Jijoy

 | 

Twwndezz

 | 

854724718

 | 

38281

 | 

640270266870

 |

As you can see, all strings were masked with random characters. You may also notice that the format of strings, such as telephone numbers, has been preserved. This approach is called `Format Preserving Hashing`. Read more about transformations [here](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#FormatPreservingHashingParams).

If we take a look at the `customer` table, it has also been changed:

Table 4. `customer` table after masking      
| customer\_id | store\_id | first\_name | last\_name | email | address\_id |
| --- | --- | --- | --- | --- | --- |
| 
2013831174

 | 

2013831174

 | 

WQEJ

 | 

YKDPW

 | 

[ZBHY.OYHYA@fgnwkhodyxxgkk.lxc](mailto:ZBHY.OYHYA@fgnwkhodyxxgkk.lxc)

 | 

15183026

 |
| 

1843733460

 | 

2013831174

 | 

XJMVRIKE

 | 

DEJECWQ

 | 

[QRODGFBA.BYBTDSM@ywjxsmntkpvozf.xyu](mailto:QRODGFBA.BYBTDSM@ywjxsmntkpvozf.xyu)

 | 

695573888

 |
| 

589262816

 | 

2013831174

 | 

GBBQA

 | 

FJENIEYU

 | 

[QOIZU.FFMEWGOU@zmrogyqpgdsapk.itw](mailto:QOIZU.FFMEWGOU@zmrogyqpgdsapk.itw)

 | 

716836103

 |
| 

376640671

 | 

1843733460

 | 

HTIXRVE

 | 

CURAO

 | 

[EYPTGBK.TPICV@puabcqlglyjttb.qmw](mailto:EYPTGBK.TPICV@puabcqlglyjttb.qmw)

 | 

1546062460

 |

Note that although numeric ids like `address_id` are masked with random numbers, in linked tables they are masked synchronously so that foreign key relations persist. This approach is called [Unique Hashing](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#UniqueHashingParams).

Sometimes we want to keep some tables as is. For that, we can use table settings overrides:

```yaml
default_config:
  mode: MASKING

tables:
  - table_name_with_schema: "public.actor"
    mode: KEEP

table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
safety_mode: RELAXED
```

Please note that the mode defined in `default_config` is applied automatically to all tables, and `tables` work as override for that global default.

Also, we can customize our masking for other tables. Let’s imagine we need more realistic names, addresses, and phone numbers in `customer` and `address` tables. For this, we can use [person\_generator](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#PersonGeneratorParams), [address\_generator](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#AddressGeneratorParams) and [formatted\_string\_generator](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#FormattedStringGeneratorParams) :

```yaml
default_config:
  mode: MASKING

tables:
  - table_name_with_schema: "public.customer"
    transformations:
      - columns: ["first_name", "last_name", "email"]
        params:
          type: "person_generator"
          column_templates: ["${first_name}", "${last_name}", "${email}"]
  - table_name_with_schema: "public.address"
    transformations:
      - columns: ["address", "district", "postal_code"]
        params:
          type: "address_generator"
          column_templates: ["${house_number} ${street_name}", "${city}", "${zip_code}"]
      - columns: ["phone"]
        params:
          type: "formatted_string_generator"
          pattern: "\\+44[0-9]{4} [0-9]{6}"

table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
safety_mode: RELAXED
```

Let’s see the result:

Table 5. `address` table after masking with improved configuration      
| address\_id | address | district | city\_id | postal\_code | phone |
| --- | --- | --- | --- | --- | --- |
| 
15183026

 | 

215 Feest Stream

 | 

Benitamouth

 | 

1693424342

 | 

NB2A 9AA

 | 

+449537 136610

 |
| 

695573888

 | 

172 Frances Run

 | 

Port Nedland

 | 

2076144198

 | 

ZX9R 4LW

 | 

+444078 471011

 |
| 

716836103

 | 

245 Hartmann Creek

 | 

West Kaylene

 | 

1505222168

 | 

UQ2W 7JW

 | 

+447869 036453

 |
| 

1546062460

 | 

281 Barton Underpass

 | 

Sethton

 | 

854724718

 | 

SL8H 5HN

 | 

+442518 225195

 |

Table 6. `customer` table after masking with improved configuration      
| customer\_id | store\_id | first\_name | last\_name | email | address\_id |
| --- | --- | --- | --- | --- | --- |
| 
2013831174

 | 

2013831174

 | 

Casie

 | 

Dooley

 | 

[dooley2605@effertzquitzonandreinger.com](mailto:dooley2605@effertzquitzonandreinger.com)

 | 

15183026

 |
| 

1843733460

 | 

2013831174

 | 

Rickie

 | 

Trantow

 | 

[trantow7645@kertzmannhaley.com](mailto:trantow7645@kertzmannhaley.com)

 | 

695573888

 |
| 

589262816

 | 

2013831174

 | 

Lynn

 | 

Kub

 | 

[kub7789@keeblerinc.com](mailto:kub7789@keeblerinc.com)

 | 

716836103

 |
| 

376640671

 | 

1843733460

 | 

Raymonde

 | 

Crona

 | 

[crona2450@padberggroup.com](mailto:crona2450@padberggroup.com)

 | 

1546062460

 |

More transformation parameters can be found [here](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations#FormattedStringGeneratorParams).

> **NOTE**
> On the [transformations](https://docs.synthesized.io/tdk/latest/user_guide/040_reference/transformers/transformations) page, you can find transformation compatibility in `modes` column. All "generators" can be used in both `MASKING` and `GENERATION` modes, but there are some purely "masking" transformations, such as `noising`, that can only be used in `MASKING` mode.

Let’s combine all examples above:

```yaml
default_config:
  mode: MASKING

tables:
  - table_name_with_schema: "public.actor"
    mode: KEEP
  - table_name_with_schema: "public.customer"
    transformations:
      - columns: ["first_name", "last_name", "email"]
        params:
          type: "person_generator"
          column_templates: ["${first_name}", "${last_name}", "${email}"]
  - table_name_with_schema: "public.address"
    transformations:
      - columns: ["address", "district", "postal_code"]
        params:
          type: "address_generator"
          column_templates: ["${house_number} ${street_name}", "${city}", "${zip_code}"]
      - columns: ["phone"]
        params:
          type: "formatted_string_generator"
          pattern: "\\+44[0-9]{4} [0-9]{6}"

table_truncation_mode: TRUNCATE
schema_creation_mode: CREATE_IF_NOT_EXISTS
safety_mode: RELAXED
```
