PostgreSQL command line colors!

Did you know that PostgreSQL tools can, under specific circumstances, display colors?
Well, I didn’t know until I came across this section in the documentation that explains it.
There are two different environment variables named PG_COLOR and PG_COLORS respectively. The first (note the singular) decides if the colors have to be activated or not, while the second contains the sequence of colors.
Clearly, colors are related to errors and other messages regarding a tool and not SQL errors!
Let’s see this in action:

As you can see, after setting PG_COLOR to always, both psql and pg_dump show the error with a red color and the message tag with a bold face. You can change the default color behaviour by setting the values in the PG_COLORS environment variable, so for example you can turn the errors to purple:

The PG_COLORS variable is a string that contains the log level (e.g., error) followed by the color code (e.g., 01;31 means bold red). The same color palette that you apply in shell and printf(2) like escape sequences can be applied to PG_COLORS variable. You can even make text blinking:

As far as I understand, the command line tools adapts to the colors thru the logging subsystem.

The article PostgreSQL command line colors! has been posted by Luca Ferrari on January 23, 2023