How to Mask Data in Oracle

Galaxy Glossary

How do I mask sensitive data in Oracle databases?

Oracle Data Redaction hides or obfuscates column values at query-time without changing the data on disk.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What problem does Oracle data masking solve?

Data masking prevents developers, analysts, and attackers from seeing real customer emails, credit-card numbers, or salaries in lower environments while letting applications continue to run normally.

How does Oracle Data Redaction work?

The DBMS_REDACT package attaches policies to tables or views. When a query matches the policy predicate, Oracle rewrites the result set, replacing sensitive column values with full, partial, or random masks.

Which columns should I mask first?

Start with PII in Customers.email, Customers.name, payment data in Orders.total_amount, and any token or API key columns. Prioritize by regulatory requirements (GDPR, PCI-DSS, HIPAA).

How do I create a redaction policy?

1 – Grant the privilege

GRANT EXEMPT REDACTION POLICY TO admin_user;

2 – Add the policy

BEGIN
DBMS_REDACT.ADD_POLICY(
object_schema => 'ECOM',
object_name => 'CUSTOMERS',
column_name => 'EMAIL',
policy_name => 'mask_email',
function_type => DBMS_REDACT.PARTIAL,
function_parameters => '0,2,NULL,@@example.com',
expression => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'')<>''ADMIN_USER'''
);
END;
/

This keeps the first two characters and replaces the domain for every non-admin session.

Can I mask multiple columns at once?

Yes. Call ADD_POLICY again for each column or pass a comma-separated list to column_name.

How do I verify the mask?

-- Session as DEV_USER
SELECT id, email FROM ecom.customers WHERE id = 42;
-- Returns: j@****@@example.com

Connect as ADMIN_USER and rerun the query to see the original value, confirming the policy works.

How do I drop or disable a policy?

Disable temporarily with DBMS_REDACT.ALTER_POLICY(..., action => DBMS_REDACT.DISABLE); or remove it permanently using DBMS_REDACT.DROP_POLICY.

What are best practices?

  • Create policies in a dedicated security schema.
  • Use predicates to exempt service accounts that need real data.
  • Document every policy and test it in staging before production.

Why How to Mask Data in Oracle is important

How to Mask Data in Oracle Example Usage


-- Mask customer email addresses for all non-admin users
BEGIN
  DBMS_REDACT.ADD_POLICY(
    object_schema       => 'ECOM',
    object_name         => 'CUSTOMERS',
    column_name         => 'EMAIL',
    policy_name         => 'cust_email_mask',
    function_type       => DBMS_REDACT.PARTIAL,
    function_parameters => '0,2,NULL,@@example.com',
    expression          => 'SYS_CONTEXT(''USERENV'',''SESSION_USER'') NOT IN (''ADMIN'')'
  );
END;
/
-- Test as app user
SELECT id, email FROM ecom.customers WHERE id = 1;

How to Mask Data in Oracle Syntax


BEGIN
  DBMS_REDACT.ADD_POLICY(
    object_schema        => '<schema>',        -- owner of the table/view
    object_name          => '<table_or_view>', -- e.g., CUSTOMERS
    column_name          => '<column>',        -- single or comma list
    policy_name          => '<policy_id>',
    function_type        => DBMS_REDACT.{FULL|PARTIAL|REGEXP|RANDOM|NULLIFY},
    function_parameters  => '<params>',        -- depends on type
    expression           => '<boolean SQL>',   -- when to apply (optional)
    policy_description   => '<text>'           -- optional comment
  );
END;
/

Common Mistakes

Frequently Asked Questions (FAQs)

Can I mask data for specific columns only?

Yes. Pass a comma-separated list of columns or call ADD_POLICY separately for each column you want to protect.

Does masking slow down queries?

Redaction adds minimal overhead because masking happens in memory after the data is fetched. Benchmarks show single-digit millisecond impact for typical OLTP queries.

Is the original data modified on disk?

No. Oracle rewrites the result set at runtime. The underlying data stays unchanged, ensuring reports and backups remain accurate.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo