PostgreSQL-加密jsonb数据 [英] Postgresql - encrypt jsonb data

查看:126
本文介绍了PostgreSQL-加密jsonb数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的postgres表中有一个jsonb列,用于存储json数据.我想以加密格式存储数据,并能够查询和获取纯文本值.有办法吗?

I have a jsonb column in my postgres table where I am storing json data. I want to store the data in an encrypted format and be able to query and get the plain text value. Is there a way to do it?

推荐答案

使用 pgcrypto 扩展.

create extension if not exists pgcrypto;

如果要将现有的jsonb列更改为已加密的列,请将列的类型更改为bytea并使用一对扩展的加密/解密函数,例如:

If you want to alter an existing jsonb column to encrypted one, change the type of the column to bytea and use a pair of the extension's encryption / decryption functions, e.g.:

create table my_table(id serial primary key, data jsonb);
insert into my_table (data) values
('{"key": "value"}');

alter table my_table
    alter data type bytea 
    using pgp_sym_encrypt(data::text, 'secret_password');

select pgp_sym_decrypt(data, 'secret_password')
from my_table;

 pgp_sym_decrypt  
------------------
 {"key": "value"}
(1 row)

这篇关于PostgreSQL-加密jsonb数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆