文章
问答
冒泡
PostgreSQL之encode和decode方法

最近在使用PostgreSQL时,需要将某些字段编码为base64字符串,正好PostgreSQL也提供了对应的方法,我使用的是PostgreSQL13,小记一下。

我有一张表为`u_user`,里面有个字段为`phone`,如果需要编码为base64字符串,则sql语句为:

select encode(cast(phone as bytea), 'base64') from u_user;

结果为:

解码语句为:

select convert_from(decode(phone, 'base64'), 'UTF-8') from u_user;

结果为:

官网中关于encode方法的描述如下:

encode ( bytes bytea, format text ) → text
Encodes binary data into a textual representation; supported format values are: base64, escape, hex.
encode('123\000\001', 'base64') → MTIzAAE=

关于decode方法的描述如下:

decode ( string text, format text ) → bytea
Decodes binary data from a textual representation; supported format values are the same as for encode.
decode('MTIzAAE=', 'base64') → \x3132330001

其中format支持三种值:base64,escape,hex

postgresql

关于作者

justin
123456
获得点赞
文章被阅读