snippetsqlMinor
How to display Postgres version in the CLI pompt?
Viewed 0 times
thepostgresversionclipompthowdisplay
Problem
I whish to display the version of the server I am connected to in the command-line (console) interface prompt. From what I read in the documentation, it is possible to execute a shell command, it is possible to display a psql variable value.
The idea is to get the server's version information on connection and use it in the client's prompt. How can I (in the
Edit: Jack Douglas♦ has the right answer by pointing out the
Thank you.
The idea is to get the server's version information on connection and use it in the client's prompt. How can I (in the
.psqlrc file ?) assign the server version in a psql variable ?Edit: Jack Douglas♦ has the right answer by pointing out the
\gset feature. It ended up with ashow server_version
\gset
\set PROMPT1 '%:server_version: >'Thank you.
Solution
Since 9.3, you can do this using \gset in psqlrc:
Or, as 'the value of the selected prompt variable is printed literally, except where a percent sign (%) is encountered':
If your version is <9.3, it's still possible, but much uglier, e.g. something like:
select split_part(version(),' ',2) pmt
\gset
\set PROMPT1 '%:pmt:'Or, as 'the value of the selected prompt variable is printed literally, except where a percent sign (%) is encountered':
select split_part(version(),' ',2) "PROMPT1"
\gsetIf your version is <9.3, it's still possible, but much uglier, e.g. something like:
\set PROMPT1 `psql postgres postgres -Atc "select split_part(version(),' ',2)"`Code Snippets
select split_part(version(),' ',2) pmt
\gset
\set PROMPT1 '%:pmt:'select split_part(version(),' ',2) "PROMPT1"
\gset\set PROMPT1 `psql postgres postgres -Atc "select split_part(version(),' ',2)"`Context
StackExchange Database Administrators Q#111780, answer score: 8
Revisions (0)
No revisions yet.