1

I perform the same sql scripts on two server with different postgres versions. The first one has postgres 9.4.4 (this works fine) and the other one 9.5 (this throws an exception) installed.

UPDATE archived_invoice SET encrypted_xml 
= encrypt(xml::bytea, 'MySuperSecretKey'::bytea, 'aes-ecb/pad:pkcs')

The exception:

Caused by: org.postgresql.util.PSQLException: ERROR: invalid input syntax for type bytea
SQL Status:22P02

Are there any differences between these two postgresql versions?

1
  • Possibly you have standard_conforming_strings differing between systems, which leads to different interpretation of backslashes. Anyway you should use the convert_to() function to get bytes from text, as opposed to the cast to bytea. A cast does interpretation of escape sequences, which is completely off base here. Commented Mar 17, 2016 at 16:50

1 Answer 1

7

PostgreSQL will throw an ERROR: invalid input syntax for type bytea at you if the text you're casting into a bytea contains an invalid escape sequence:

# select '\i is not a valid escape sequence'::bytea;
ERROR:  invalid input syntax for type bytea
LINE 1: select '\i is not a valid escape sequence'::bytea;

I'm guessing your "xml" column contains or may contain some backslashes. This is fine for XML and text columns, but presents an issue when casting to bytea.

You will need to escape the backslashes:

UPDATE archived_invoice SET encrypted_xml 
= encrypt(
    replace(xml, '\', '\\')::bytea,
    'MySuperSecretKey'::bytea,
    'aes-ecb/pad:pkcs'
)
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thx! This is really strange. My script is working on windows but not on ubuntu. Why is this a problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.