Customize psqlrc to be different based on host or username Hi I would like to have my DB prompt a different color if I am logged into production or with a user with full permissions.

So I would like something like this (which doesn't work) in my psqlrc:

if [%M == PROD]; then
     \set PROMPT1 '%[%033[1;35m%]%M:PROD_IS_RED%[%033[0m%] %n@%/%R%#%x '
else
     \set PROMPT1 '%[%033[1;32m%]%M:NORMAL%[%033[0m%] %n@%/%R%#%x '
fi                                                                                                                


Is there any way to make something like this work ? Is there some alternative can I make a script that runs psql with a different psqlrc file? like psql --psqlrc_file=psqlrc2
You can use \gset (introduced in 9.3) to query information about the server and save it in a psql variable, then set the prompt to include that variable.

select case when inet_server_addr()='10.0.0.34' then 'PROD' else 'TEST' end as host \gset
\set PROMPT1 '%:host: %/%x%x%x%R%# '


I don't know how you get this introduce color, rather than just text, in my hands your color control sequences are just presented literally.

If you want different .psqlrc files, you have to set it as an environment variable as their is no command line option.  So something like:

alias psql_prod='PSQLRC=~/.psqlrc_prod psql'