It might be easier to use a text format rather than binary (so just remove WITH BINARY ). The text format has the best documentation and is designed for better interoperability. The binary format is more suitable for moving between postgres installations, and even there they have version incompatibility.
The text format will write the bytea field as if it were text, and encode any non-printable characters with octal representation \nnn (except for a few special cases that it encodes with C \x style patterns such as \n and \t and t .d.). They are listed in the COPY documentation.
The only caveat to this is that you must be absolutely sure that the character encoding you use is the same when saving the file while reading. To make sure the printed characters match the same numbers. I stick with SQL_ASCII as this makes the process easier.
source share