Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a string into a bytea column

I want to insert text data into a Postgres bytea column using the concat function or the || operator. I am getting an error

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');

I am executing the insert inside a stored procedure. If want to add the argument like

(concat('abac-' , row.name , 'test123'));

How do I do it?

like image 558
Arav Avatar asked Oct 18 '25 10:10

Arav


1 Answers

Perform a type cast after concatenating the values:

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

Note: The difference between || and concat is how they behave with NULLs.

like image 93
Laurenz Albe Avatar answered Oct 20 '25 04:10

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!