"--variable foo=bar" vs. "\set foo quux" in ~/.psqlrc - Mailing list pgsql-general

From Julian Mehnle
Subject "--variable foo=bar" vs. "\set foo quux" in ~/.psqlrc
Date
Msg-id 201005142042.30519.julian@mehnle.net
Whole thread Raw
Responses Re: "--variable foo=bar" vs. "\set foo quux" in ~/.psqlrc  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hi all,

I'm trying to add the database host name to my psql prompts.  The obvious
solution is to add %M or %m to the PROMPT{1,2} variables in ~/.psqlrc.

However I have to work with a few databases that can be reached only
through SSH tunnels, for which I use aliases like this:

  alias dbfoo='ssh -q -f -o "ExitOnForwardFailure yes" -L 15432:server-beyond-tunnel:5432
               gateway-host sleep 5; psql -h localhost -p 15432 -U dbuser foodb'

So the problem with the above prompt solution is that this will show a
hostname of "localhost" because that's what psql connects to.

Now I've tried using the following in .psqlrc:

  \set host :HOST
  \set PROMPT1 '%:host::%/%x%R%# '
  \set PROMPT2 '%:host::%/%x%R%# '

and then overriding the "host" variable from the command line using
"--variable host=server-beyond-tunnel".  Unfortunately the --variable
option doesn't seem to have any effect, presumably because it's evaluated
*before* .psqlrc is read and executed.

Can anyone confirm that --variable command-line options are evaluated
before .psqlrc is read and executed?  If so, does anyone know the
rationale for that?  It seems counterintuitive to me, as it makes
overriding variables from the command-line impossible.

If there is consensus that evaluating --variable options *after* .psqlrc
was read and executed is an acceptable change, or that a --variable-late
option should be introduced, I might come up with a patch.

TIA,

-Julian

Attachment

pgsql-general by date:

Previous
From: Richard Broersma
Date:
Subject: ALTER DOMAIN feature request
Next
From:
Date:
Subject: Re: Poor query performance on one of two "like" databases in production.