gpt4 book ai didi

json - Postgresql - 加密jsonb数据

转载 作者:行者123 更新时间:2023-11-29 11:44:27 26 4
gpt4 key购买 nike

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

最佳答案

使用 pgcrypto扩展名。

create extension if not exists pgcrypto;

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

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)

关于json - Postgresql - 加密jsonb数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com