Thread: psql \conninfo in tabular form?

psql \conninfo in tabular form?

From
Ron
Date:
v13.10

Instead of a sentence like this:
You are connected to database "postgres" as user "postgres" via socket in 
"/var/run/postgresql" at port "5433".

I'd rather have something tabular like:
    keyword  |   value
------------+-----------------------
   database  | postgres
   user      | postgres
   host      | /var/run/postgresql
   port      |  5433

Running psql with the -E option didn't help.  There's a github project named 
ibarwick/conninfo which looked useful, but it seems to only parse connection 
strings.

-- 
Born in Arizona, moved to Babylonia.



Re: psql \conninfo in tabular form?

From
Erik Wienhold
Date:
> On 07/03/2023 18:58 CET Ron <ronljohnsonjr@gmail.com> wrote:
>
> v13.10
>
> Instead of a sentence like this:
> You are connected to database "postgres" as user "postgres" via socket in
> "/var/run/postgresql" at port "5433".
>
> I'd rather have something tabular like:
>     keyword  |   value
> ------------+-----------------------
>    database  | postgres
>    user      | postgres
>    host      | /var/run/postgresql
>    port      |  5433

Define variable conninfo with the query in ~/.psqlrc:

    \set conninfo 'select * from (values (''database'', current_database()), (''user'', session_user), (''host'',
coalesce(inet_server_addr()::text,current_setting(''unix_socket_directories''))), (''port'',
coalesce(inet_server_port()::text,current_setting(''port'')))) t(keyword, value);' 

And run it like that:

    postgres=# :conninfo
     keyword  |      value
    ----------+-----------------
     database | postgres
     user     | ewie
     host     | /run/postgresql
     port     | 5432
    (4 rows)

--
Erik