Skip to content

Latest commit

 

History

History
48 lines (34 loc) · 1.6 KB

0091_how_to_format_text_output_in_psql_scripts.md

File metadata and controls

48 lines (34 loc) · 1.6 KB

Originally from: tweet, LinkedIn post.


How to format text output in psql scripts

I post a new PostgreSQL "howto" article every day. Join me in this journey – subscribe, provide feedback, share!

For psql's \echo command, using colors and basic text formatting such as bold or underlined can be done through the use of ANSI color codes. This can be useful when building complex scripts for psql (example: postgres_dba).

Examples:

\echo '\033[1;31mThis is red text\033[0m'
\echo '\033[1;32mThis is green text\033[0m'
\echo '\033[1;33mThis is yellow text\033[0m'
\echo '\033[1;34mThis is blue text\033[0m'
\echo '\033[1;35mThis is magenta text\033[0m'
\echo '\033[1;36mThis is cyan text\033[0m'
\echo '\033[1mThis text is bold\033[0m'
\echo '\033[4mThis text is underlined\033[0m'
\echo '\033[38;2;128;0;128mThis text is purple\033[0m'

-- RGB – arbitrary color
\echo '\033[38;2;255;100;0mThis text is in orange (RGB 255,100,0)\033[0m'

Result:

Important: the \033[0m sequence resets the text formatting to the default.

The formatting is preserved in non-interactive mode of psql, and when combined with ts (to prefix timestamps, included in the moreutils package in Ubuntu):

psql -Xc "\echo '\033[1;35mThis is magenta text\033[0m'" | ts

When less is used, formatting doesn't work – but it can be solved with option -R:

psql -Xc "\echo '\033[1;35mThis is magenta text\033[0m'" | less -R