Postgres. How to convert string that contains '\' to bytea?

3.4k views Asked by At

I have string 'test\data' or just one backslash symbol '\'.

How it convert to bytea?

1

There are 1 answers

0
FireEmerald On

Backlashes need special handling if casted from to bytea see src/backend/utils/adt/varlena.c.

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 not IMMUTABLE and thus it can't be used in any context out of the box.