6

I have an image database. With an image table with two bytea columns. I would like to query the table and see these columns to be able to see whether or not these entries are storing images or not. As they are nullable columns.

Currently when I query the DB using psql command line, my whole command line screen goes blank trying to display the bytea.

I've googled around for a while now and can't find a way to display the table suitably. Ideally the first 'x' characters would display.

I can confirm that the bytea will print if small enough by uploading a tiny tiny picture.

This is what I see when I do: SELECT * FROM Image;

my view when querying this table

3 Answers 3

16

you can use the encode function:

select encode(bytea_column, 'hex')
from image;

If you only want to see the first bytes, just use the left() function on that:

select left(encode(bytea_column, 'hex'), 40)
from image;

More details in the manual:
http://www.postgresql.org/docs/current/static/functions-binarystring.html

1
  • Thanks for this. It's exactly what I wanted. :D I found that for very large images even encoding to hex wasn't enough. But limiting to the first 40 chars was what I needed :D
    – tjheslin1
    Oct 31, 2014 at 14:47
3

If you are not trying to interpret binary data yourself, why not just do:

select length(img1), length(img2) from Image

or

select img1 is null, img2 is null from Image
1
  • Thanks for these suggestions. I marked the other as the answer as I said "Ideally the first 'x' characters would display" in my question. However these are great alternatives :D
    – tjheslin1
    Oct 31, 2014 at 14:48
2

You can toggle expanded formatting mode with the following command

\x
1
  • 1
    Yep, this is all about the formatting of the output in psql, not about the queries. Oct 30, 2014 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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