1

In question Update multiple rows in same query using PostgreSQL Roman Peckar gave an answer similar to this; I have modified it for the purpose of my question:

update test as t set
  column_a = c.column_a,
  column_b = c.column_b
from (values
  ('123', bytea1),   
  ('345', bytea2)  
) as c(column_a, column_b) 
where c.column_a = t.column_a;

In my case table test has a column of type bytea, say column_b. However, this does not work as c.column_b is of type text and thus an error is produced saying there is no conversion from text to bytea and hinting to use a cast. Well, using a cast does not help either as another error occurs about encoding referring to a LATIN encoding. I apologise for the imprecise reporting of the errors but I do not presently have access to the machine on which this work was carried out.

It seems that the default type of the c.column_b is text. Cannot the type of a column be dictated in the 'as' clause say, 'as c(column_a, column_b type bytea)' or in some other way? If not I assume I must resort to using some binary string function which seems a bit inelegant to say the least.

2
  • 1
    it would be great to see sqlfiddle.com example, so it would be easy for us to create working solution
    – roman
    Commented Nov 2, 2013 at 14:36
  • @Roman Pekar: Well, you could of told me of sqlfiddle before this! :-). I'm just a poor C++ programmer having to deal with sql yet again. OK, see sqlfiddle.com/#!1/11bb0/2 for the error. So we use a cast, sqlfiddle.com/#!1/a0ba6/3 ok, no error, that's because we are entering a properly escaped bytea value. But, with this last query my code is sending a stream of raw byte data and I'm getting an error that moans about the locale (LATIN1 irc) and cannot convert the raw bytes. What's going on?
    – UglyCoder
    Commented Nov 2, 2013 at 18:55

1 Answer 1

1

Because text type is for text. It needs properly encoded text in your client encoding and which can be saved with no data loss in your server encoding (so for example in latin1 no or , as characters like this can not be saved using this encoding).

So if you need to save text, which can contain characters outside of latin1 (like anything typed to a web form) you'd need to change database encoding to utf-8. Or, as a last resort, use encode(data,'base64').

3
  • I do not understand your answer. I am saving bytea data to column_b, type bytea, of table test via column_b, type text (so the error is telling me), of 'table' c. If I could declare the type of column_b of table c as bytea then there would be no problem (I hope to believe!).
    – UglyCoder
    Commented Nov 3, 2013 at 11:22
  • Data casted to bytea in your SQLfiddle works.
    – Tometzky
    Commented Nov 3, 2013 at 11:41
  • It does, as I have explained above in my reply to Roman Pekar's comment. It does not work when I am sending that SQL statement with data that can contain any character i.e. binary data.
    – UglyCoder
    Commented Nov 3, 2013 at 14:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.