Thus escape each backslash using replace('test\data', '\', '\\')::bytea before casting to bytea.
You could also use the already suggested function convert_to(text, encoding) bytea. But note that this function is notIMMUTABLE and thus it can't be used in any context out of the box.
Backlashes need special handling if casted from to
bytea
seesrc/backend/utils/adt/varlena.c
.Thus escape each backslash using
replace('test\data', '\', '\\')::bytea
before casting tobytea
.You could also use the already suggested function
convert_to(text, encoding) bytea
. But note that this function is notIMMUTABLE
and thus it can't be used in any context out of the box.