> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bytebase.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Configure Dynamic Data Masking

Bytebase [Dynamic Data Masking](/security/data-masking/overview/) can mask sensitive data in
the query result based on the context on the fly. It helps organizations to **protect sensitive data**
from being exposed to unauthorized users.

<img src="https://mintcdn.com/dbx/pTzXQHTiyMQ5jmdS/content/docs/security/data-masking/masking-overview.webp?fit=max&auto=format&n=pTzXQHTiyMQ5jmdS&q=85&s=268fd4ade2da575be5c5ecbff2647f5c" alt="masking-overview" width="1680" height="776" data-path="content/docs/security/data-masking/masking-overview.webp" />

<Info>
  This tutorial covers configuration via UI console. You can also codify the masking policies, check
  out [this sample](https://github.com/bytebase/example-database-security/tree/main/masking).
</Info>

## Prerequisites

* [Docker](https://www.docker.com/)
* Bytebase Enterprise plan, you can request a free trial [here](https://www.bytebase.com/contact-us/)

## Preparation

1. Make sure your [Docker](https://www.docker.com/) is running, and start the Bytebase Docker container with command:

   ```bash theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null} theme={null}
   docker run --rm --init \
     --name bytebase \
     --publish 8080:8080 --pull always \
     --volume ~/.bytebase/data:/var/opt/bytebase \
     bytebase/bytebase:latest
   ```

2. Having Bytebase successfully running in Docker, visit it via `localhost:8080`. Register an admin account and it will be granted the `workspace admin` role automatically.

3. Acquire the Enterprise license. Enter **Instances** on the left. Select both instances to **Assign License**.

### No Masking

Enter **SQL Editor** on top right. Without any worksheet open (no tab page open on top), click **Connect to a database** or **Select a database to start**.

<img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/sql-editor-entry.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=219958d4acbb44b4625884a993260e92" alt="sql-editor-entry" width="2090" height="874" data-path="content/docs/tutorials/data-masking/sql-editor-entry.webp" />

Choose database `hr_prod` under `Prod Sample Instance` within the Connection detail page. Run `SELECT * FROM employee;`, you'll see the following result without any masking.

<img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/prod-without-masking.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=9d02ec16f1a5e593f4e9782694caaa7b" alt="prod-without-masking" width="1634" height="984" data-path="content/docs/tutorials/data-masking/prod-without-masking.webp" />

### Semantic Types

Semantic type is a way to classify data into different categories. You may apply them to columns to apply the same masking rule.

1. Enter **Data Access > Semantic Types**, click **Add**.
2. Fill in the name, description and specify the **Masking Algorithm**. Click the check mark.

<img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-semantic-type.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=8b585294f9c8175900f6f37e701bd073" alt="bb-semantic-type" width="1986" height="434" data-path="content/docs/tutorials/data-masking/bb-semantic-type.webp" />

There are two ways to apply the semantic type and its masking algorithm:

1. Apply to a column directly.
2. Apply according to a global masking rule.

### Column Masking Rule

As a project owner, you may want to apply the semantic type to a column, usually it's a column that you want to mask on production environment.

1. Go into the project `Sample Project`, and click database `hr_prod`.

2. Go into the `employee` table, and set the `birth_date` column to apply the `birth_date` semantic type.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-column-semantic-type.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=cabca5fafffb09ebb541adf9d6d3ebf4" alt="bb-column-semantic-type" width="2396" height="1078" data-path="content/docs/tutorials/data-masking/bb-column-semantic-type.webp" />

3. Go back to SQL Editor, run `SELECT * FROM employee;` within `hr_prod`. You'll see the `birth_date` is masked based on the masking algorithm.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-sql-editor-column-masking.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=f987c6719ecbe8907b61c1c5618ce804" alt="bb-sql-editor-column-masking" width="1740" height="948" data-path="content/docs/tutorials/data-masking/bb-sql-editor-column-masking.webp" />

### Global Masking Rule

As a DBA, you may want to batch apply masking settings. Use [Global Masking Rule](/security/data-masking/global-masking-rule/) to achieve this.

1. Enter **Data Access > Semantic Types**, click **Use Predefined Type**.

2. Add the predefined type `Default`.

3. Enter **Data Access > Global Masking Rule**, click **Add**.

4. Here define a global masking rule to mask all the `birth_date` columns in all tables on production environment with semantic type `Default`, and click **Confirm**.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-global-masking-rule.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=c688c6ee9030277e29290396e6fdb07b" alt="bb-global-masking-rule" width="1744" height="262" data-path="content/docs/tutorials/data-masking/bb-global-masking-rule.webp" />

5. Go back to SQL Editor, run `SELECT * FROM employee;` within `hr_prod`. You'll see the `birth_date` is masked.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-sql-editor-column-masking.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=f987c6719ecbe8907b61c1c5618ce804" alt="bb-sql-editor-column-masking" width="1740" height="948" data-path="content/docs/tutorials/data-masking/bb-sql-editor-column-masking.webp" />

6. You may also notice that the global masking rule take precedence over the column masking rule.

### Export data with masked columns

Exported data is masked in the same way as query results.

1. Stay on the SQL Editor after querying, and click **Export**.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-sql-editor-export.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=e380bcaa0c5088bb6b842e4621dc1447" alt="bb-sql-editor-export" width="1744" height="1020" data-path="content/docs/tutorials/data-masking/bb-sql-editor-export.webp" />

2. Fill in the export rows number, choose the format and click **Confirm**. The file will start downloading.

3. Open the downloaded file, you'll see the `birth_date` is masked.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/exported-data.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=b631044095db0e744d4efad5d99240a2" alt="exported-data" width="1082" height="706" data-path="content/docs/tutorials/data-masking/exported-data.webp" />

### Masking Exemptions

You can reveal masked data to a specific user by granting masking exemption.

1. Go into the project `Sample Project`, and click **Manage > Masking Exemptions**.

2. Grant exemption to the user and click **Confirm**.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-masking-exemption.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=7512c3d7ac0848a6986400dec894bebc" alt="bb-masking-exemption" width="2506" height="1368" data-path="content/docs/tutorials/data-masking/bb-masking-exemption.webp" />

3. Go back to SQL Editor, run `SELECT * FROM employee;` within database `hr_prod`. You'll see the `birth_date` is unmasked.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/bb-sql-editor-exemption.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=dde02f9101817b86cb6d9dc773f6a3b9" alt="bb-sql-editor-exemption" width="2002" height="1106" data-path="content/docs/tutorials/data-masking/bb-sql-editor-exemption.webp" />

4. If you export the data, the `birth_date` is also unmasked.

   <img src="https://mintcdn.com/dbx/wzfDanOtVHdPe4nL/content/docs/tutorials/data-masking/exported-data-exemption.webp?fit=max&auto=format&n=wzfDanOtVHdPe4nL&q=85&s=d0dd62ef83b5b6dc6809da99f425855b" alt="exported-data-exemption" width="1208" height="702" data-path="content/docs/tutorials/data-masking/exported-data-exemption.webp" />

## Related content

* [Docs for Dynamic Data Masking](/security/data-masking/overview/)
* [Just-in-Time Database Access](/tutorials/just-in-time-database-access-part1/)
