我试图弄清楚如何在 Postgres 中以最佳方式“匿名化”数据。
假设为数据库中的每个项目都创建了模式。在模式中,项目完成后可能会有需要随机化/删除/匿名的列。
我正在考虑创建新域以用于可能包含敏感数据的所有列,并且在项目完成后只需运行小脚本即可。
我提出了一些遵循这个逻辑的小解决方案
create domain anonymous_text as text;
create table test (
username anonymous_text,
address anonymous_text
);
create table test2 (
username anonymous_text,
address anonymous_text
);
insert into test values('hello', 'world');
insert into test2 values('foo', 'bar');
do $$
declare
arow record;
begin
for arow in
select
'update '||c.table_name||' set '||c.column_name||' = md5((random() * '||c.ordinal_position||')::text);'
as my_update_query
from
(select
table_name, column_name, ordinal_position
from information_schema.columns
where domain_name = 'anonymous_text' and table_schema='myschema') c
loop
execute arow.my_update_query;
end loop;
end;
$$;
我的问题是。有没有人遇到过类似的问题,有没有更简单的方法可以在 Postgres 中实现这个结果?