What I mean with all this is that I can't use any other format because I don't own the database.
What I see for the
binarydata column is that it is a JPEG/JFIF binary encoded as C-style string constant according to
https://www.postgresql.org/docs/9.2/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS , 4.1.2.2. The only thing is it seems to be encoded twice (?!). I can see double backslashes (\\). So if we replace them with a single backslash (\) and replace the double single quote ('') with a single single quote (sorry for that) we should get a C-style string again. Un-escaped once more (for the octal values: e.g. \\377\\330 --> \337\330 --> #$FF#$D8) it will become the binary image.
The only thing that disturbs me is that if we escape a single quote (') twice, we must get four quotes ('''') but in your sample we have occurrences of only two ('').
The opposite conversion should be straightforward: convert PNG to JPG, with LCL it is quite easy, I believe, and then, for each byte from the JPEG binary stream:
- If it is a single quote - append two single quotes
- If it is printable ASCII - just append it
- If it is not - append two backslashes, then three digits of the byte octal representation
Edit: Just to be on the safe side - obviously you can encode any ambiguous symbol with two backslashes and three octal digits, including the backslash, single quote, etc.