pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Who am I? Where am I connected?
9+ messages / 7 participants
[nested] [flat]

* Who am I? Where am I connected?
@ 2022-05-18 10:07  Dominique Devienne <ddevienne@gmail.com>
  0 siblings, 5 replies; 9+ messages in thread

From: Dominique Devienne @ 2022-05-18 10:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

LibPQ has various defaults for the host, user, and DB name.
There's also the password file, the service file and service name.
In the example below, I can connect with a "naked" psql invocation.

Once connected, can I find out all aspects of the connection string?
Or where they came from, like a pgpass.conf or service file?

How to get the host, port, db name once connected?
SHOW and pg_settings does not appear to be it, at first glance.

Thanks, --DD

c:\Users\ddevienne>psql
psql (12.1, server 14.2)
WARNING: psql major version 12, server major version 14.
         Some psql features might not work.
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

ddevienne=>





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

* Re: Who am I? Where am I connected?
@ 2022-05-18 10:29  Erik Wienhold <ewie@mailbox.org>
  parent: Dominique Devienne <ddevienne@gmail.com>
  4 siblings, 0 replies; 9+ messages in thread

From: Erik Wienhold @ 2022-05-18 10:29 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; pgsql-general@lists.postgresql.org

Hi Dominique,

you can use \conninfo in psql to show the database, user, host (or socket in my example), and port:

ewie@desktop ~ $ psql test
Null display is "".
psql (14.3)
Type "help" for help.

test=# \conninfo
You are connected to database "test" as user "ewie" via socket in "/run/postgresql" at port "5432".


	- Erik


> On 18/05/2022 12:07 Dominique Devienne <ddevienne@gmail.com> wrote:
> 
>  
> LibPQ has various defaults for the host, user, and DB name.
> There's also the password file, the service file and service name.
> In the example below, I can connect with a "naked" psql invocation.
> 
> Once connected, can I find out all aspects of the connection string?
> Or where they came from, like a pgpass.conf or service file?
> 
> How to get the host, port, db name once connected?
> SHOW and pg_settings does not appear to be it, at first glance.
> 
> Thanks, --DD
> 
> c:\Users\ddevienne>psql
> psql (12.1, server 14.2)
> WARNING: psql major version 12, server major version 14.
>          Some psql features might not work.
> WARNING: Console code page (437) differs from Windows code page (1252)
>          8-bit characters might not work correctly. See psql reference
>          page "Notes for Windows users" for details.
> Type "help" for help.
> 
> ddevienne=>





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

* Re: Who am I? Where am I connected?
@ 2022-05-18 10:30  Ganesh Korde <ganeshakorde@gmail.com>
  parent: Dominique Devienne <ddevienne@gmail.com>
  4 siblings, 0 replies; 9+ messages in thread

From: Ganesh Korde @ 2022-05-18 10:30 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

 \conninfo will show you desired details

Regards,
Ganesh Korde.

On Wed, 18 May 2022, 3:38 pm Dominique Devienne, <ddevienne@gmail.com>
wrote:

> LibPQ has various defaults for the host, user, and DB name.
> There's also the password file, the service file and service name.
> In the example below, I can connect with a "naked" psql invocation.
>
> Once connected, can I find out all aspects of the connection string?
> Or where they came from, like a pgpass.conf or service file?
>
> How to get the host, port, db name once connected?
> SHOW and pg_settings does not appear to be it, at first glance.
>
> Thanks, --DD
>
> c:\Users\ddevienne>psql
> psql (12.1, server 14.2)
> WARNING: psql major version 12, server major version 14.
>          Some psql features might not work.
> WARNING: Console code page (437) differs from Windows code page (1252)
>          8-bit characters might not work correctly. See psql reference
>          page "Notes for Windows users" for details.
> Type "help" for help.
>
> ddevienne=>
>
>
>

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

* Re: Who am I? Where am I connected?
@ 2022-05-18 10:32  Christoph Moench-Tegeder <cmt@burggraben.net>
  parent: Dominique Devienne <ddevienne@gmail.com>
  4 siblings, 0 replies; 9+ messages in thread

From: Christoph Moench-Tegeder @ 2022-05-18 10:32 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

## Dominique Devienne (ddevienne@gmail.com):

> Once connected, can I find out all aspects of the connection string?

\conninfo in psql (pro tip: \? actually helps), "Connection Status
Functions" https://www.postgresql.org/docs/current/libpq-status.html
in libpq; and in a pinch you could find your connection in
pg_stat_activity (pid = pg_backend_pid()) plus some assorted queries
for other details. You cannot get actual secrets like your private
ssl key or your password (the server most likely doesn't have that,
and the client assumes that you have it).

Regards,
Christoph

-- 
Spare Space





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

* Re: Who am I? Where am I connected?
@ 2022-05-18 15:31  Dominique Devienne <ddevienne@gmail.com>
  parent: Dominique Devienne <ddevienne@gmail.com>
  4 siblings, 0 replies; 9+ messages in thread

From: Dominique Devienne @ 2022-05-18 15:31 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Wed, May 18, 2022 at 12:07 PM Dominique Devienne <ddevienne@gmail.com> wrote:
> LibPQ has various defaults for the host, user, and DB name.
> There's also the password file, the service file and service name.
> In the example below, I can connect with a "naked" psql invocation.
>
> Once connected, can I find out all aspects of the connection string?

Thank you all for \conninfo.

I was more thinking at the time about the SQL-way to get that info,
not the psql way.
But thanks to https://www.postgresql.org/docs/current/functions-info.html
I managed
to emulate it, modulo resolving the server's IP into a hostname.

ddevienne=> \conninfo
You are connected to database "ddevienne" as user "ddevienne" on host
"localhost" (address "::1") at port "5432".

ddevienne=> select current_database() || ' ' || session_user || ' ' ||
inet_server_addr() || ':' || inet_server_port();
             ?column?
----------------------------------
 ddevienne ddevienne ::1/128:5432
(1 row)

> Or where they came from, like a pgpass.conf or service file?

OTOH, no one replied to that part of the question.

How to know if the user or database name was defaulted?
Or came from a service-file, using a given service name?
Is there no way, except by reverse-engineering the logic of the
env-vars and built-in defaults?





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

* Re: Who am I? Where am I connected?
@ 2022-05-18 15:43  David G. Johnston <david.g.johnston@gmail.com>
  parent: Dominique Devienne <ddevienne@gmail.com>
  4 siblings, 1 reply; 9+ messages in thread

From: David G. Johnston @ 2022-05-18 15:43 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wed, May 18, 2022 at 3:08 AM Dominique Devienne <ddevienne@gmail.com>
wrote:

> Once connected, can I find out all aspects of the connection string?
> Or where they came from, like a pgpass.conf or service file?
>
> How to get the host, port, db name once connected?
> SHOW and pg_settings does not appear to be it, at first glance.
>
>
The server has no clue how the values sent to it came into existence - nor
should it.

Whether and how any particular client might expose this kind of debugging
information (or upgrade it to proper state info) is up to the client.  I do
not know what options psql offers.

David J.

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

* Re: Who am I? Where am I connected?
@ 2022-05-18 21:59  Dominique Devienne <ddevienne@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Dominique Devienne @ 2022-05-18 21:59 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wed, May 18, 2022 at 5:43 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Wed, May 18, 2022 at 3:08 AM Dominique Devienne <ddevienne@gmail.com>
> wrote:
>
>> Once connected, can I find out all aspects of the connection string?
>> Or where they came from, like a pgpass.conf or service file?
>>
>> How to get the host, port, db name once connected?
>> SHOW and pg_settings does not appear to be it, at first glance.
>>
>>
> The server has no clue how the values sent to it came into existence - nor
> should it.
>
> Whether and how any particular client might expose this kind of debugging
> information (or upgrade it to proper state info) is up to the client.  I do
> not know what options psql offers.
>

AFAIK, it’s not psql that does this though, it’s libpq the official client
api.
And the fact libpq has no way to surface that info seems like an important
oversight.

>

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

* Re: Who am I? Where am I connected?
@ 2022-05-18 22:37  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Dominique Devienne <ddevienne@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Adrian Klaver @ 2022-05-18 22:37 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On 5/18/22 14:59, Dominique Devienne wrote:
> 

> 
> AFAIK, it’s not psql that does this though, it’s libpq the official 
> client api.
> And the fact libpq has no way to surface that info seems like an 
> important oversight.
> 

Not all clients use libpq e.g. the Postgres JDBC driver. It just uses 
the Postgres  Frontend/Backend protocol:

https://www.postgresql.org/docs/current/protocol.html


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: Who am I? Where am I connected?
@ 2022-05-18 23:12  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Dominique Devienne <ddevienne@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Tom Lane @ 2022-05-18 23:12 UTC (permalink / raw)
  To: Dominique Devienne <ddevienne@gmail.com>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Dominique Devienne <ddevienne@gmail.com> writes:
> AFAIK, it’s not psql that does this though, it’s libpq the official client
> api.
> And the fact libpq has no way to surface that info seems like an important
> oversight.

PQconninfo() will show you all the option values in use by a connection
object.  It's true that it's not very easy to tell where any given value
came from, but I doubt it'd be worth an ABI break to add such info.
(You could at least recognize values that match the hard-wired default
or environment variable value.)

			regards, tom lane





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


end of thread, other threads:[~2022-05-18 23:12 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 10:07 Who am I? Where am I connected? Dominique Devienne <ddevienne@gmail.com>
2022-05-18 10:29 ` Erik Wienhold <ewie@mailbox.org>
2022-05-18 10:30 ` Ganesh Korde <ganeshakorde@gmail.com>
2022-05-18 10:32 ` Christoph Moench-Tegeder <cmt@burggraben.net>
2022-05-18 15:31 ` Dominique Devienne <ddevienne@gmail.com>
2022-05-18 15:43 ` David G. Johnston <david.g.johnston@gmail.com>
2022-05-18 21:59   ` Dominique Devienne <ddevienne@gmail.com>
2022-05-18 22:37     ` Adrian Klaver <adrian.klaver@aklaver.com>
2022-05-18 23:12     ` Tom Lane <tgl@sss.pgh.pa.us>

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