Обсуждение: connecting to localhost -> ::1 client_addr ?

Поиск
Список
Период
Сортировка

connecting to localhost -> ::1 client_addr ?

От
Luca Ferrari
Дата:
Hi all,
this may be tribial, but I'm seeing connections from ::1 in
pg_stat_activity, and I've never realiuzed that psql converts
"localhost" in IPv6.
Is there a way to "force" the hostname localhost to appear as IPv4 or
am I missing something?
I've tested it also with a Perl program, and specifying localhost is
providing ::1 too. I suspect this is mainly due to the host name
resolution, but I'm not sure.


luca@venkman ~ % psql -h localhost -U luca -c 'SELECT
pid,usename,client_addr FROM pg_stat_activity
WHERE pid = pg_backend_pid();' testdb
pid | usename | client_addr
-----+---------+-------------
895 | luca    | ::1
(1 row)

luca@venkman ~ % psql -h 127.0.0.1 -U luca -c 'SELECT
pid,usename,client_addr FROM pg_stat_activity
WHERE pid = pg_backend_pid();' testdb
pid | usename | client_addr
-----+---------+-------------
901 | luca    | 127.0.0.1
(1 row)

luca@venkman ~ % psql -h venkman -U luca -c 'SELECT
pid,usename,client_addr FROM pg_stat_activity WH
ERE pid = pg_backend_pid();' testdb
pid | usename |  client_addr
-----+---------+----------------
905 | luca    | 192.168.222.13
(1 row)

luca@venkman ~ % psql 'user=luca host=localhost dbname=testdb' -c
'SELECT pid,usename,client_addr FR
OM pg_stat_activity WHERE pid = pg_backend_pid();'
pid | usename | client_addr
-----+---------+-------------
943 | luca    | ::1
(1 row)

luca@venkman ~ % psql 'user=luca host=localhost dbname=testdb' -c
'SELECT version();'
                                                version

----------------------------------------------------------------------------------------------------
------
PostgreSQL 14.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.2.1
20220127 (Red Hat 11.2.1-9), 6
4-bit
(1 row)



Re: connecting to localhost -> ::1 client_addr ?

От
Magnus Hagander
Дата:
On Fri, May 5, 2023 at 9:23 AM Luca Ferrari <fluca1978@gmail.com> wrote:
>
> Hi all,
> this may be tribial, but I'm seeing connections from ::1 in
> pg_stat_activity, and I've never realiuzed that psql converts
> "localhost" in IPv6.
> Is there a way to "force" the hostname localhost to appear as IPv4 or
> am I missing something?
>
> I've tested it also with a Perl program, and specifying localhost is
> providing ::1 too. I suspect this is mainly due to the host name
> resolution, but I'm not sure.

This is something that's part of your resolver library on your machine.

You can for example edit /etc/hosts to remove ::1. Or you can edit
/etc/gai.conf and change the priority order to put ipv4 first, that
should work as well I believe. (Those are assuming you're on Linux).

Or of course, you can configure listen_addresses to only have ipv4 in
it -- but then ipv6 won't work for *any* connections, it won't just
change the priority.

//Magnus