1. 程式人生 > >PostgreSQL脫敏示例

PostgreSQL脫敏示例

mydb=# create table test_desensitization(id integer, name varchar(32), phone_num varchar(11));
CREATE TABLE
mydb=# insert into test_desensitization select num, 'name_'||num, 18500000000+(random()*90000000)::int from generate_series(1, 100) g(num);;
INSERT 0 100
mydb=# create table test_desensitization_result as select id, substring(name, 1, 2)||'******'||substring(name, length(name), 1) as name, substring(phone_num, 1, 3)||'****'||substring(phone_num, length(phone_num) -3, 4) as phone_num from test_desensitization ;
SELECT 100
mydb=# select * from test_desensitization_result ;
 id  |   name    |  phone_num
-----+-----------+-------------
   1 | na******1 | 185****9782
   2 | na******2 | 185****2419
   3 | na******3 | 185****5163
   4 | na******4 | 185****9263
   5 | na******5 | 185****1941
   6 | na******6 | 185****0390
   7 | na******7 | 185****2158
   8 | na******8 | 185****8646
   9 | na******9 | 185****9253
  10 | na******0 | 185****4371