Last Updated: February 25, 2016
· creaktive

Prettify records with long columns in PostgreSQL shell

The chances are, once in a while, your psql terminal session ends up looking like this:

screenshot #1

Fixing the line wrapping is very straight-forward, just issue the following commands (or put them into ~/.psqlrc):

\setenv PAGER less
\setenv LESS -IS
\pset linestyle unicode

The last line is optional, but draws a lot nicer tables. Now, the same query results as above will appear almost bearable:

screenshot #2

This still sucks if the only thing want is to copy one record to your editor or IM window. It would be easier to have one-line-per-column. MySQL shell has \G for that. psql has \x\g\x, so instead of:

select * from brasil_ibge where uf = 'SP' limit 10;

Just use:

select * from brasil_ibge where uf = 'SP' limit 10 \x\g\x

And there you are!

screenshot #3