Below are screenshots and configs for yet another color prompting of psql. The goal was to get the color prompting scheme that works well on both light and dark background terminals.

Here is an example of .bashrc file:


export YELLOW=`echo -e '\033[1;33m'`
export LIGHT_CYAN=`echo -e '\033[1;36m'`
export GREEN=`echo -e '\033[0;32m'`
export NOCOLOR=`echo -e '\033[0m'`
export LESS="-iMSx4 -FXR"
export PAGER="sed \"s/^\(([0-9]\+ [rows]\+)\)/$GREEN\1$NOCOLOR/;s/^\(-\[\ RECORD\ [0-9]\+\ \][-+]\+\)/$GREEN\1$NOCOLOR/;s/|/$GREEN|$NOCOLOR/g;s/^\([-+]\+\)/$GREEN\1$NOCOLOR/\" 2>/dev/null | less"

And here is a .psqlrc example:

\set QUIET 1
\set ON_ERROR_ROLLBACK interactive
\set VERBOSITY verbose
\x auto
\set PROMPT1 '%[%033[38;5;27m%]%`hostname -s`%[%033[38;5;102m%]/%/ %[%033[31;5;27m%]%`/var/lib/pgsql/`%[%033[0m%] %# '
\set PROMPT2 ''
\set HISTFILE ~/.psql_history- :DBNAME
\set HISTCONTROL ignoredups
\pset null [null]
\pset pager always
\unset QUIET

Script for determining host role (/var/lib/pgsql/ is really simple:


res=`psql postgres -t -A -c 'show transaction_read_only;'`
if [ $res == 'off' ]; then
    echo 'M'
    echo 'R'

And here are the screenshots of such psql prompt: Colorized psql for dark backgrounds

Colorized psql for light backgrounds



comments powered by Disqus