3

I enjoy using psql with Tmux and Vim, using Vim to edit my queries and send them to psql in another Tmux pane. This is a fantastic workflow, far superior to a GUI like DBeaver in my opinion, but:

It doesn't take many columns for psql to be unable to show the results nicely, and wrapped lines in a psql output looks disastrous.

So far my options are:

Extended mode

With \x you can see rows one at a time shown more like a JSON object. This is kind of OK but is terrible for comparing rows against one another.

Make the font smaller

With some merry bashing of ctrl - I can sometimes zoom out far enough to get the results to show on a single line. Needless to say, this isn't really a workable solution

Make tmux chop the ends off long lines

As shown here it's possible to get Tmux to truncate rows instead of wrapping them.

This is great if I'm only interested in the first few columns. But it's not really a sustainable way of working. What if I want to compare columns at the end?


Can anyone think of any avenues I might explore that won't make my GUI-using colleagues laugh at me for my terribly inadequate way of viewing wide tables?

It doesn't have to be specific to psql or to Tmux. Maybe just a way to make the terminal wider generally.

2
  • less -S lets you scroll left/right with the arrow keys. – meuh Jul 3 '20 at 10:17
  • @meuh This sounds promising... – LondonRob Jul 3 '20 at 10:34
1

Consider pspg - pager created by PostgreSQL developer Pavel Stěhule.

This pager was designed primarily for terminal database clients. It has many features handy for paging tabular data like pinning columns while scrolling right. pspg can be used with other db clients or even csv files like shown in the example at its github project page.

To start using pspg, install it and export PAGER or PSQL_PAGER in your environment (taken from project page):

#for Postgres 10 and older
export PAGER="pspg"

#for postgres 11 and newer
export PSQL_PAGER="pspg"

or add following to ~/.psqlrc

\setenv PAGER pspg

You might also be interested in opening pager in split pane, while keeping your psql shell ready for other query.

~/bin/pspg-wrapper

#!/usr/bin/env bash
if [[ $TMUX ]]
then
    tempfile=$(mktemp)
    cat - > "$tempfile"
    tmux splitw -p 50 "pspg $tempfile"
    unlink "$tempfile"
else # 
    pspg -F
fi

~/.psqlrc

\setenv PAGER ~/bin/pspg-wrapper

If psql is running in tmux then open pspg in split pane otherwise just run pspg. -F means print on stdout directly, if the output fits to single screen

4
  • OMG I 😍😍😍 this a lot. Excellent. – LondonRob Nov 18 '20 at 16:11
  • Just the pspg or sending output to the split pane? I’m just curious 🙂 – Jakub Jindra Nov 18 '20 at 16:14
  • I didn't do the split pane thing. My split-pane setup is already pretty complicated (because I usually have Vim open with vim-tmux-runner to send the queries. – LondonRob Nov 18 '20 at 16:17
  • 1
    For future reference, my setup in .psqlrc is: \setenv PAGER 'pspg -s 17 --only-for-tables --quit-if-one-screen -X --vertical-cursor'. Seems like sensible defaults to me. – LondonRob Nov 18 '20 at 16:17
1

As per this comment we can get left/right scrolling by using less -S as our pager.

Since psql respects the PAGER envvar, a nice low-impact way to achieve left/right scrolling might be to create an alias like:

alias psql="PAGER='less -S' psql"

This allows the left/right keys to be used to move through wide tables in psql output. This looks like a pretty nice solution to me.

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.