Postgresql - how to convert bytea to text or string in Java

chrismarx picture chrismarx · Mar 17, 2012 · Viewed 8.5k times · Source

I have a column that is bytea in postgres, and I can use a statement like this to get this kind of output:

select encode(mycolumn,'escape') from personTable;

and get:

254\355\000?sr\000?java.util.HashMap??\332\301\303?`\321?\000?F\000
loadFactorI\000 thresholdxp?@\000\000\000\000\000?w?\000\000\000?\000\000\000?t\000?      
userIdt\000
USER2200000x

How would I see this in plain text? It is indeed storing a java.util.HashMap-

Answer

ruakh picture ruakh · Mar 17, 2012

This is really a Java issue rather than a PostgreSQL issue. personTable.mycolumn is holding a byte-string that was created using the Java Serialization API, and to retrieve the original object that the byte-string represents, you need to use that same API. (See "Discover the secrets of the Java Serialization API" for sample code.)