Search code examples
postgresqlcommand-promptpsql

Colors in the psql prompt


I get an trash after the prompt when I flick through the history when I add colors to my prompt.

I have tried the proposed color prompt command from the postgres doc:

\set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '

I have also tried a lot of other prompt examples that i found with google but with the same result.

So after setting the prompt I have a yellow prompt something like this:

ulf@mydb=#

I then run the query:

ulf@mydb=# select * from country;

Then I flick through the history with . This would bring me the select-line ones more, but instead I get:

ulf@mydb=# \set PROMPT1 'select * from country;

Note the \set... before the selectwhich is a remnant from the initial promt-setting.

I also get similar result if I put the \set PROMPT1-command in .pslqrc

I get the same result in both xterm and gnome-terminal. I have my bash-prompt colored in a similar way but I have no problems with that prompt.

What have I done wrong?


Solution

  • Note: I have yet to find the root cause of why this is necessary sometimes, however, this should fix it for you.

    There are two characters to indicate start/stop of non-printable characters in readline/libedit (?), 0x01 and 0x02.

    So, if you add these before and after the non-printable sequences in your PROMPT1, it should work. The result in your case would be:

    \set PROMPT1 '%001%[%033[1;33;40m%]%002%n@%/%R%001%[%033[0m%]%002%# '
    

    Update: It would seem this should be handled in psql though, somewhere they must get lost some how. Maybe USE_READLINE isn't defined? See: Line 286 in src/bin/psql/prompt.c