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
byteaseesrc/backend/utils/adt/varlena.c.Thus escape each backslash using
replace('test\data', '\', '\\')::byteabefore casting tobytea.You could also use the already suggested function
convert_to(text, encoding) bytea. But note that this function is notIMMUTABLEand thus it can't be used in any context out of the box.