Convert Bytea to text in PostgreSQL

One of my friend asked me a question that , He has stored XML data in a table with bytea column and he want to get the data back as XML. We can use the “encode function” to get the same.


postgres=# create table test(
name bytea);
CREATE TABLE


postgres=# insert into test values (”);
INSERT 0 1

postgres=# insert into test values (‘Srinivas’);
INSERT 0 1

postgres=# select * from test;
           name
————————–
 \x3c584d4c3e3c2f584d4c3e
 \x5372696e69766173
(2 rows)

postgres=#
postgres=# select encode(name,’escape’) from test;
   encode
————-
 
 Srinivas
(2 rows)

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s