pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
HOST variable in psql
5+ messages / 2 participants
[nested] [flat]

* HOST variable in psql
@ 2020-04-27 08:03 Paul Förster <paul.foerster@gmail.com>
  2020-04-27 12:20 ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Paul Förster @ 2020-04-27 08:03 UTC (permalink / raw)
  To: pgsql-general

Hi,

the use of :HOST in psql containing the socket directory for local connections is pretty useless for us, so we set it to the hostname in a global psqlrc file:

\set HOST `hostname -s`

This works great when invoking psql on the command line. But if I perform a \c to connect to a different database in the database cluster, then :HOST gets reset to the socket directory. On the one hand, this makes sense, on the other, the global psqlrc file does not seem to be executed again, thus leaving :HOST in an unuable state for us:

postgres=# \echo :HOST
mypghost
postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# \echo :HOST
/tmp

The documentations (https://www.postgresql.org/docs/12/app-psql.html) says:

"HOST
The database server host you are currently connected to. This is set every time you connect to a database (including program start-up), but can be changed or unset."

"Files
psqlrc and ~/.psqlrc
Unless it is passed an -X option, psql attempts to read and execute commands from the system-wide startup file (psqlrc) and then the user's personal startup file (~/.psqlrc), after connecting to the database but before accepting normal commands."

Makes sense so far, except the last part "after connecting to the database but before accepting normal commands." This applies only to the first psql startup but not a subsequent \c. If I do a \c, then that is connecting to a database and that means, the psqlrc *should* be executed again, which it isn't.

Bug or feature? If feature, is there any remedy to this behavior?

Thanks in advance,
Paul




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOST variable in psql
  2020-04-27 08:03 HOST variable in psql Paul Förster <paul.foerster@gmail.com>
@ 2020-04-27 12:20 ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-04-27 12:44   ` Re: HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2020-04-27 12:20 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: pgsql-general

=?utf-8?Q?Paul_F=C3=B6rster?= <paul.foerster@gmail.com> writes:
> the use of :HOST in psql containing the socket directory for local connections is pretty useless for us, so we set it to the hostname in a global psqlrc file:
> \set HOST `hostname -s`
> This works great when invoking psql on the command line. But if I perform a \c to connect to a different database in the database cluster, then :HOST gets reset to the socket directory. On the one hand, this makes sense, on the other, the global psqlrc file does not seem to be executed again, thus leaving :HOST in an unuable state for us:

AFAICS, psql is behaving as documented.  Why are you trying to override
HOST like that, instead of just using some other variable?

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOST variable in psql
  2020-04-27 08:03 HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  2020-04-27 12:20 ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-04-27 12:44   ` Paul Förster <paul.foerster@gmail.com>
  2020-04-27 13:13     ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Paul Förster @ 2020-04-27 12:44 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

Hi Tom,

> On 27. Apr, 2020, at 14:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> AFAICS, psql is behaving as documented.  Why are you trying to override
> HOST like that, instead of just using some other variable?

I could but

a) HOST is HOST and should not point to a socket directory. A socket directory is not a HOST. This makes no sense, especially since I see no use for using a socket directory in any psql script. There are tons of use cases for the hostname, though.

b) it would require changing some scripts.

c) the docs are IMHO not 100% clear on this. I think the psqlrc should be executed on EVERY new connect, hence including \c, and this is how I interpret the docs too. But it doesn't...

also (slightly related) d) PostgreSQL unfortunately does not provide any functionality to determine the server hostname it runs on. There is an extension but I still don't see why PostgreSQL does not include this natively.

Cheers,
Paul




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOST variable in psql
  2020-04-27 08:03 HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  2020-04-27 12:20 ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
  2020-04-27 12:44   ` Re: HOST variable in psql Paul Förster <paul.foerster@gmail.com>
@ 2020-04-27 13:13     ` Tom Lane <tgl@sss.pgh.pa.us>
  2020-04-27 13:38       ` Re: HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2020-04-27 13:13 UTC (permalink / raw)
  To: Paul Förster <paul.foerster@gmail.com>; +Cc: pgsql-general

=?utf-8?Q?Paul_F=C3=B6rster?= <paul.foerster@gmail.com> writes:
>> On 27. Apr, 2020, at 14:20, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> AFAICS, psql is behaving as documented.  Why are you trying to override
>> HOST like that, instead of just using some other variable?

> I could but

> a) HOST is HOST and should not point to a socket directory. A socket directory is not a HOST. This makes no sense, especially since I see no use for using a socket directory in any psql script. There are tons of use cases for the hostname, though.

[ shrug... ]  When connecting via a Unix socket, psql does not have a
relevant "hostname" to report, so it reports what it's got.  You are
ascribing a meaning to HOST that it does not have, never has had, and
never can have in this case.

> c) the docs are IMHO not 100% clear on this. I think the psqlrc should be executed on EVERY new connect, hence including \c, and this is how I interpret the docs too. But it doesn't...

There has been some talk of providing a way to execute stuff on reconnect,
if memory serves, but nothing's been done about it.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: HOST variable in psql
  2020-04-27 08:03 HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  2020-04-27 12:20 ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
  2020-04-27 12:44   ` Re: HOST variable in psql Paul Förster <paul.foerster@gmail.com>
  2020-04-27 13:13     ` Re: HOST variable in psql Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-04-27 13:38       ` Paul Förster <paul.foerster@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Paul Förster @ 2020-04-27 13:38 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

Hi Tom,

> On 27. Apr, 2020, at 15:13, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> [ shrug... ]  When connecting via a Unix socket, psql does not have a
> relevant "hostname" to report, so it reports what it's got.  You are
> ascribing a meaning to HOST that it does not have, never has had, and
> never can have in this case.

I'm not much of a C programmer, but there's AFAIK a C function to get the hostname. So it should IMHO be easy to put something into the psql init (and \c) code that calls the function and set the HOST variable accordingly. IMHO...

> There has been some talk of providing a way to execute stuff on reconnect,
> if memory serves, but nothing's been done about it.

thought so. So I'll have to work around that.

Thanks,
Paul




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2020-04-27 13:38 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-04-27 08:03 HOST variable in psql Paul Förster <paul.foerster@gmail.com>
2020-04-27 12:20 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-04-27 12:44   ` Paul Förster <paul.foerster@gmail.com>
2020-04-27 13:13     ` Tom Lane <tgl@sss.pgh.pa.us>
2020-04-27 13:38       ` Paul Förster <paul.foerster@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox