postgresql_anonymizer使用

来源:这里教程网 时间:2026-03-14 20:45:07 作者:

 瀚高数据库

目录
环境
文档用途
详细信息
 
 
环境
系统平台:Linux x86-64 Red Hat Enterprise Linux 7
版本:12
 
文档用途

postgresql_anonymizer是对数据库中的个人识别信息或商业敏感数据进行屏蔽或替换的扩展。

 
详细信息

1. 介绍

postgresql_anonymizer是对数据库中的个人识别信息或商业敏感数据进行屏蔽或替换的扩展。该扩展使用标准sql语句定义规则,内置多种屏蔽规则函数。依据定义的规则有3中使用方式 

Anonymous Dumps : 将屏蔽数据导出到SQL文件中

Static Masking : 根据规则移除替换敏感数据(此方法慎用,避免数据被替换而造成丢失。)

Dynamic Masking : 依据规则屏蔽隐藏敏感数据

2. 安装

支持多种安装方式,包括rpm,pgxn,docker等。建议使用源码安装方式。 下载,编译

git clone https://gitlab.com/dalibo/postgresql_anonymizer.git
make extension PG_CONFIG=/opt/pg1211/bin/pg_config
sudo make install PG_CONFIG=/opt/pg1211/bin/pg_config

配置加载扩展

ALTER DATABASE postgres SET session_preload_libraries = 'anon';
pg_ctl restart  --重启生效

创建扩展

CREATE EXTENSION anon CASCADE;create extension pgcrypto ;

初始化扩展

SELECT anon.init();

 

3.配置

 用于声明屏蔽规则的函数必须位于指定的模式内,默认是pg_catalog和anon

ALTER DATABASE postgres SET anon.restrict_to_trusted_schemas = on;
 

声明屏蔽规则,数据屏蔽规则仅通过使用security%20labels来声明

声明屏蔽规则(MASKED WITH FUNCTION需要大写)
security label for anon on column test_mask.name is 'MASKED WITH FUNCTION anon.fake_last_name()';
删除屏蔽规则
security label for anon on column test_mask.name is null;
删除所有规则
SELECT anon.remove_masks_for_all_columns();

 

4.屏蔽策略介绍

共8种。参考官方文档描述  Masking%20Functions 

Destruction

Adding%20Noise%20#是数据进行一定幅度的变化。对于数值和日期,Adding%20Noise通常很有趣

Randomization

Faking%20#使用随机但看似合理的数据替换敏感数据。对于姓名和其他“直接标识符”,Faking通常很有用

Advanced%20Faking

Pseudonymization

Generic%20Hashing

Partial%20scrambling%20#对部分数据进行遮挡。非常适合用于电子邮件地址和电话号码

Generalization

 示例:Faking

为了使用faking函数,必须先加载init()扩展。
SELECT anon.init(); 
返回通用的名字
security label for anon on column test_mask.name is 'MASKED WITH FUNCTION anon.fake_first_name()';

Adding%20Noise

返回的值是原始值随机+/-20%
security label for anon on column test_mask.salary is 'MASKED WITH FUNCTION anon.noise(original_value,0.2)'; 
返回的值是原始值随机+/-7天
security label for anon on column test_mask.hiredate is 'MASKED WITH FUNCTION anon.dnoise(original_value,7 days)';

 Partial%20scrambling

返回值显示后四位,其他以xxxx代替
security label for anon on column test_mask.telephone is 'MASKED WITH FUNCTION anon.partial(telephone,2,$$*****$$$$,4)';

 

5.%20Static%20Masking静态屏蔽使用

永久删除敏感数据

应用屏蔽规则,对整个数据库
SELECT anon.anonymize_database();
应用屏蔽规则,对指定表
SELECT anon.anonymize_table('public.test_mask'); 
应用屏蔽规则,对指定列
SELECT anon.anonymize_column('customer','zipcode');

 注意,数据会被替换,适用于测试数据脱敏。

6.%20Dynamic%20Masking动态屏蔽使用

对“屏蔽”用户隐藏敏感数据

开启动态屏蔽
SELECT anon.start_dynamic_masking(); 
声明屏蔽用户
SECURITY LABEL FOR anon ON ROLE test IS 'MASKED';
解除用户屏蔽
SECURITY LABEL FOR anon ON ROLE bob IS NULL; 
解除所有用户屏蔽
SELECT anon.remove_masks_for_all_roles();

 动态屏蔽使用限制 

drop表需要使用CASCADE

psql命令\dt%20无法显示被屏蔽表信息

只能屏蔽一个schema模式下的数据,默认是public,可修改为其他shema,但只能屏蔽一个模式

会使查询性能非常低,特别是join表时

使用图形化工具是,访问屏蔽表信息会报错,ERROR:%20permission%20denied%20for%20table%20foo

7.%20Anonymous%20Dumps导出屏蔽数据

由于屏蔽设置,不能使用pg_dump导出数据。需要使用pg_dump_anon.sh

pg_dump_anon.sh -h localhost -U postgres -d postgres -t test_dy_mask > /tmp/test_dy_mask_anon.sql

8. 相关字典试图

pg_seclabels

pg_seclabel

9. 使用示例

创建表
create table test_dy_mask (id int,name varchar(22),salary int,hiredate timestamp,telephone text); 
insert into test_dy_mask values (1,'max',20000,'2022-06-21 14:00:00','15512345678');
postgres=# select * from test_dy_mask ;
 id | name | salary |      hiredate       |  telephone
----+------+--------+---------------------+-------------
  1 | aaa  |  20000 | 2022-06-21 14:00:00 | 15512345678
(1 row)
声明屏蔽规则
SELECT anon.init(); --使用faking
security label for anon on column test_dy_mask.name is 'MASKED WITH FUNCTION anon.fake_first_name()'; --faking
security label for anon on column test_dy_mask.salary is 'MASKED WITH FUNCTION anon.noise(salary,0.2)'; --add noise
security label for anon on column test_dy_mask.telephone is 'MASKED WITH FUNCTION anon.partial(telephone,2,$$*****$$,4)';  --Partial scrambling
使用动态屏蔽
SELECT anon.start_dynamic_masking();  --开启动态屏蔽
create user test with password 'test';  --创建一个新用户
SECURITY LABEL FOR anon ON ROLE test IS 'MASKED'; --声明屏蔽用户
grant select on test_dy_mask to test; --授权
select * from test_dy_mask;  --使用屏蔽用户查询数据
查看结果
postgres=> select * from test_dy_mask ;
 id | name | salary |      hiredate       |  telephone
----+------+--------+---------------------+-------------
  1 | Koby |  16618 | 2022-06-21 14:00:00 | 15*****5678
(1 row)

相关推荐