bytea display as hex

Posted on Thursday, September 20, 2012



Bytea display as hex

I recently had to deal with a postgres database that had a column with data type bytea.  You can find out more information on bytea from http://www.postgresql.org/docs/9.0/static/datatype-binary.html [1]

Now programmatically querying the database gives you an ascii byte stream, which works out just fine.  But if you are just doing a simple command line query it will display each byte as an ascii character.  And for those characters that cannot be displayed for example byte 00000000  which in ascii is NUL, it will display it in an escaped format for example \000  (it’s in octet and always 3 numbers)

So a simple query might return something like this.

pd\323\362r