Обсуждение: issue with reading hostname

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

issue with reading hostname

От
Atul Kumar
Дата:
Hi,

I have postgresql  version 15 running on centos7.

I have below query that reads hostname from /tmp directory:

psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT pg_is_in_recovery();'


so below are my questions:

1. Is the psql client reading the socket file that resides in the /tmp directory to fetch the hostname ?

2. I saw the socket file in /tmp and it is empty. Then how is the psql client still reading the socket file successfully for hostname ?


this is my socket looks ( the size is 0 as the file is empty):

srwxrwxrwx.  1 postgres postgres      0 Apr 22 12:47 .s.PGSQL.5432


Please help me clarify these doubts.



Regards.






Re: issue with reading hostname

От
Adrian Klaver
Дата:
On 4/22/24 13:13, Atul Kumar wrote:
> Hi,
> 
> I have postgresql  version 15 running on centos7.
> 
> I have below query that reads hostname from /tmp directory:
> 
> psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT pg_is_in_recovery();'
> 
> 
> so below are my questions:
> 
> 1. Is the psql client reading the socket file that resides in the /tmp 
> directory to fetch the hostname ?
> 
> 2. I saw the socket file in /tmp and it is empty. Then how is the psql 
> client still reading the socket file successfully for hostname ?
> 
> 
> this is my socket looks ( the size is 0 as the file is empty):
> 
> srwxrwxrwx. 1 postgres postgres      0 Apr 22 12:47 .s.PGSQL.5432
> 
> 
> Please help me clarify these doubts.

https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS

"host

     Name of host to connect to. If a host name looks like an absolute 
path name, it specifies Unix-domain communication rather than TCP/IP 
communication; the value is the name of the directory in which the 
socket file is stored. (On Unix, an absolute path name begins with a 
slash. On Windows, paths starting with drive letters are also 
recognized.) If the host name starts with @, it is taken as a 
Unix-domain socket in the abstract namespace (currently supported on 
Linux and Windows). The default behavior when host is not specified, or 
is empty, is to connect to a Unix-domain socket in /tmp (or whatever 
socket directory was specified when PostgreSQL was built). On Windows, 
the default is to connect to localhost.

     A comma-separated list of host names is also accepted, in which 
case each host name in the list is tried in order; an empty item in the 
list selects the default behavior as explained above. See Section 
34.1.1.3 for details.
"

The simplistic explanation is that the socket is the "host".

> 
> 
> 
> Regards.
> 
> 
> 
> 
> 
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com




Re: issue with reading hostname

От
Ron Johnson
Дата:
On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar <akumar14871@gmail.com> wrote:
Hi,

I have postgresql  version 15 running on centos7.

I have below query that reads hostname from /tmp directory:

psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT pg_is_in_recovery();'


If you installed from the PGDG repository (possibly also the CENTOS repos, but I'm not sure), then the domain socket also lives in :
/var/run/postgresql

* I find that more expressive than /tmp.
* No need to specify the host when using sockets.
* Using a socket name makes parameterizing the hostname easier in scripts.


Re: issue with reading hostname

От
Atul Kumar
Дата:
Can we edit the socket to change the hostname in it ?

Regards.

On Tue, Apr 23, 2024 at 2:41 AM Ron Johnson <ronljohnsonjr@gmail.com> wrote:
On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar <akumar14871@gmail.com> wrote:
Hi,

I have postgresql  version 15 running on centos7.

I have below query that reads hostname from /tmp directory:

psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT pg_is_in_recovery();'


If you installed from the PGDG repository (possibly also the CENTOS repos, but I'm not sure), then the domain socket also lives in :
/var/run/postgresql

* I find that more expressive than /tmp.
* No need to specify the host when using sockets.
* Using a socket name makes parameterizing the hostname easier in scripts.


Re: issue with reading hostname

От
Ron Johnson
Дата:
There's no hostname in a socket directory.

Do you mean :"change the socket name from /tmp or /var/run/postgresql to something else?"


On Mon, Apr 22, 2024 at 5:37 PM Atul Kumar <akumar14871@gmail.com> wrote:
Can we edit the socket to change the hostname in it ?

Regards.

On Tue, Apr 23, 2024 at 2:41 AM Ron Johnson <ronljohnsonjr@gmail.com> wrote:
On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar <akumar14871@gmail.com> wrote:
Hi,

I have postgresql  version 15 running on centos7.

I have below query that reads hostname from /tmp directory:

psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT pg_is_in_recovery();'


If you installed from the PGDG repository (possibly also the CENTOS repos, but I'm not sure), then the domain socket also lives in :
/var/run/postgresql

* I find that more expressive than /tmp.
* No need to specify the host when using sockets.
* Using a socket name makes parameterizing the hostname easier in scripts.


Re: issue with reading hostname

От
Adrian Klaver
Дата:
On 4/22/24 14:37, Atul Kumar wrote:
> Can we edit the socket to change the hostname in it ?

On Ubuntu 22.04 install, given:

srwxrwxrwx  1 postgres postgres    0 Apr 22 14:01 .s.PGSQL.5432=
-rw-------  1 postgres postgres   68 Apr 22 14:01 .s.PGSQL.5432.lock

The contents of .s.PGSQL.5432.lock(the file that indicates a Postgres 
instance has a lock on the socket) are:

862
/var/lib/postgresql/15/main
1713795311
5432
/var/run/postgresql

There is no hostname to be changed as you are working with a local socket.

> 
> Regards.
> 
> On Tue, Apr 23, 2024 at 2:41 AM Ron Johnson <ronljohnsonjr@gmail.com 
> <mailto:ronljohnsonjr@gmail.com>> wrote:
> 
>     On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar <akumar14871@gmail.com
>     <mailto:akumar14871@gmail.com>> wrote:
> 
>         Hi,
> 
>         I have postgresql  version 15 running on centos7.
> 
>         I have below query that reads hostname from /tmp directory:
> 
>         psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT
>         pg_is_in_recovery();'
> 
> 
>     If you installed from the PGDG repository (possibly also the CENTOS
>     repos, but I'm not sure), then the domain socket also lives in :
>     /var/run/postgresql
> 
>     * I find that more expressive than /tmp.
>     * No need to specify the host when using sockets.
>     * Using a socket name makes parameterizing the hostname easier in
>     scripts.
> 
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com




Re: issue with reading hostname

От
Atul Kumar
Дата:
I mean, Once I change the hostname then how will the socket read the new hostname ? Does it require a postgres service restart ?



On Tue, Apr 23, 2024 at 3:19 AM Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 4/22/24 14:37, Atul Kumar wrote:
> Can we edit the socket to change the hostname in it ?

On Ubuntu 22.04 install, given:

srwxrwxrwx  1 postgres postgres    0 Apr 22 14:01 .s.PGSQL.5432=
-rw-------  1 postgres postgres   68 Apr 22 14:01 .s.PGSQL.5432.lock

The contents of .s.PGSQL.5432.lock(the file that indicates a Postgres
instance has a lock on the socket) are:

862
/var/lib/postgresql/15/main
1713795311
5432
/var/run/postgresql

There is no hostname to be changed as you are working with a local socket.

>
> Regards.
>
> On Tue, Apr 23, 2024 at 2:41 AM Ron Johnson <ronljohnsonjr@gmail.com
> <mailto:ronljohnsonjr@gmail.com>> wrote:
>
>     On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar <akumar14871@gmail.com
>     <mailto:akumar14871@gmail.com>> wrote:
>
>         Hi,
>
>         I have postgresql  version 15 running on centos7.
>
>         I have below query that reads hostname from /tmp directory:
>
>         psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT
>         pg_is_in_recovery();'
>
>
>     If you installed from the PGDG repository (possibly also the CENTOS
>     repos, but I'm not sure), then the domain socket also lives in :
>     /var/run/postgresql
>
>     * I find that more expressive than /tmp.
>     * No need to specify the host when using sockets.
>     * Using a socket name makes parameterizing the hostname easier in
>     scripts.
>
>

--
Adrian Klaver
adrian.klaver@aklaver.com

Re: issue with reading hostname

От
"David G. Johnston"
Дата:
On Mon, Apr 22, 2024 at 1:14 PM Atul Kumar <akumar14871@gmail.com> wrote:


so below are my questions:

1. Is the psql client reading the socket file that resides in the /tmp directory to fetch the hostname ?

2. I saw the socket file in /tmp and it is empty. Then how is the psql client still reading the socket file successfully for hostname ?



Unix sockets are not a PostgreSQL-specific thing, they are an OS thing.  You should be able to find information on them if you search but it isn't something our documentation is on the hook to teach.

But, in many ways, it is just a file name (Unix philosophy, everything is a file) that programs can hook their standard input and standard output up to such that the standard output sent by one process is directed to the standard input of the other process.  All on the fly, nothing gets persisted into the physical file.  It's a tin-can-and-string solution to interprocess communication.

David J.

Re: issue with reading hostname

От
"David G. Johnston"
Дата:
On Mon, Apr 22, 2024 at 2:54 PM Atul Kumar <akumar14871@gmail.com> wrote:
I mean, Once I change the hostname then how will the socket read the new hostname ? Does it require a postgres service restart ?


-h doesn't strictly mean hostname (the wording choice here does seem problematic), rather it is simply a direct specification of how and where the psql client looks for and connects to the server.  When the specification is a filename it is that file that is used and hostname and any other networking-related stuff is immaterial.  Though the server does choose to borrow the port number 5432 from networking to construct the unique name out of convenience.

David J.

Re: issue with reading hostname

От
Adrian Klaver
Дата:
On 4/22/24 14:54, Atul Kumar wrote:
> I mean, Once I change the hostname then how will the socket read the new 
> hostname ? Does it require a postgres service restart ?

The host name of the machine?

> 
> 
> 
> On Tue, Apr 23, 2024 at 3:19 AM Adrian Klaver <adrian.klaver@aklaver.com 
> <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 4/22/24 14:37, Atul Kumar wrote:
>      > Can we edit the socket to change the hostname in it ?
> 
>     On Ubuntu 22.04 install, given:
> 
>     srwxrwxrwx  1 postgres postgres    0 Apr 22 14:01 .s.PGSQL.5432=
>     -rw-------  1 postgres postgres   68 Apr 22 14:01 .s.PGSQL.5432.lock
> 
>     The contents of .s.PGSQL.5432.lock(the file that indicates a Postgres
>     instance has a lock on the socket) are:
> 
>     862
>     /var/lib/postgresql/15/main
>     1713795311
>     5432
>     /var/run/postgresql
> 
>     There is no hostname to be changed as you are working with a local
>     socket.
> 
>      >
>      > Regards.
>      >
>      > On Tue, Apr 23, 2024 at 2:41 AM Ron Johnson
>     <ronljohnsonjr@gmail.com <mailto:ronljohnsonjr@gmail.com>
>      > <mailto:ronljohnsonjr@gmail.com
>     <mailto:ronljohnsonjr@gmail.com>>> wrote:
>      >
>      >     On Mon, Apr 22, 2024 at 4:14 PM Atul Kumar
>     <akumar14871@gmail.com <mailto:akumar14871@gmail.com>
>      >     <mailto:akumar14871@gmail.com
>     <mailto:akumar14871@gmail.com>>> wrote:
>      >
>      >         Hi,
>      >
>      >         I have postgresql  version 15 running on centos7.
>      >
>      >         I have below query that reads hostname from /tmp directory:
>      >
>      >         psql postgres -A -t -p 5432 -h /tmp/ -c 'SELECT
>      >         pg_is_in_recovery();'
>      >
>      >
>      >     If you installed from the PGDG repository (possibly also the
>     CENTOS
>      >     repos, but I'm not sure), then the domain socket also lives in :
>      >     /var/run/postgresql
>      >
>      >     * I find that more expressive than /tmp.
>      >     * No need to specify the host when using sockets.
>      >     * Using a socket name makes parameterizing the hostname easier in
>      >     scripts.
>      >
>      >
> 
>     -- 
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com