Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL psql command line display bytea column

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

like image 942
tjheslin1 Avatar asked Oct 30 '14 21:10

tjheslin1


3 Answers

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

like image 123
a_horse_with_no_name Avatar answered Oct 17 '22 05:10

a_horse_with_no_name


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
like image 38
jjanes Avatar answered Oct 17 '22 07:10

jjanes


You can toggle expanded formatting mode with the following command

\x
like image 35
Bruno Calza Avatar answered Oct 17 '22 05:10

Bruno Calza