2025 May 2
How to view contents of a gzipped bytea column using psql in plaintext
Or at least this is how I did it at work =D
I was in a remote development environment and wanted to inspect the contents of a bytea column containing gzipped JSON data. Here's what I did:
$ apt-get update $ apt-get install xxd $ apt-get install jq $ psql database_name -t -c "select encoded_column from table_name where id=1;" | xxd -r -p | gunzip | jq
- gunzip was already available to decompress from gzip
- xxd is a hexdump utility that can convert between hex and binary
- jq can do a lot of things with json but here I am just using it to pretty-print.
- The -t option for psql removes the headers and just outputs the result directly
- The -c option for psql runs the query and outputs the result to standard out without starting a psql session