将字符串插入 bytea 列

Insert a string into a bytea column

我想使用 concat 函数或 || 运算符将文本数据插入 Postgres bytea 列。我收到一个错误

column "name" is of type bytea but expression is of type text
create table test(
   name bytea
);

insert into test(name) values(concat('abac-' , 'test123'));
insert into test(name) values('aa' || 'bb');

我正在存储过程中执行插入。如果要添加

之类的参数
(concat('abac-' , row.name , 'test123'));

我该怎么做?

您需要将两个字符串都转换为 bytea,例如:

insert into test(name) values('aa'::bytea || 'bb'::bytea);

连接值后执行类型转换:

INSERT INTO test (name)
   VALUES (CAST('abac-' || row.name || 'test123' AS bytea));

注意:||concat 之间的区别在于它们处理 NULL 的方式。