From: Erik Wienhold <ewie@ewie.name>
To: Markus Pilman <markus@pilman.ch>
Cc: pgsql-general@postgresql.org
Subject: Re: Binding Postgres to port 0 for testing
Date: Sat, 25 Mar 2023 22:09:02 +0100 (CET)
Message-ID: <446372806.461162.1679778542118@office.mailbox.org> (raw)
In-Reply-To: <CALavoow=fvELBK8UP11XvgvoJoMk1JEqNSoVRDQGr27-6-0GEw@mail.gmail.com>
References: <CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com>
<1804501070.456751.1679767819698@office.mailbox.org>
<CALavoow=fvELBK8UP11XvgvoJoMk1JEqNSoVRDQGr27-6-0GEw@mail.gmail.com>
> On 25/03/2023 20:10 CET Markus Pilman <markus@pilman.ch> wrote:
>
> Thanks for the suggestions. I didn't know about pg_virtualenv, that's
> interesting. Though it seems to achieve something similar as to containerize
> the test (potentially in a more platform independent way). Though it seems
> pg_virtualenv is mostly doing what my test driver is currently doing. Trying
> out the ports is obviously possible, but it seems a bit hacky to me (though
> if there's no better way I don't think that's a good show-stopper).
You can of course also use Docker and have it map port 5432 to a random host
port. Use docker-port to find the mapped host port:
docker port CONTAINER 5432/tcp
Testcontainers may also be an option if you want to use Docker:
* https://www.testcontainers.org/modules/databases/postgres/
* https://testcontainers-python.readthedocs.io/en/latest/postgres/README.html> But I am still wondering: Is there a reason PostgreSQL doesn't allow me to
> bind against port 0? I understand that in a production environment this is
> almost never the thing you want to do, but I wouldn't consider this option
> very dangerous.
One reason for not allowing port zero is Postgres' naming convention of Unix
domain sockets. The port number is included in the socket filename.
https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-UNIX-SOCKET-DIRECTORIES
Accepting port zero for a Unix domain socket would not behave the same as
binding a TCP socket to port zero.
Another benefit is that the bound port number is available through the config.
Postgres does not have to keep track of any "random" port number picked by the
operating system.
--
Erik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: ewie@ewie.name, markus@pilman.ch
Subject: Re: Binding Postgres to port 0 for testing
In-Reply-To: <446372806.461162.1679778542118@office.mailbox.org>
References: <CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com> <1804501070.456751.1679767819698@office.mailbox.org> <CALavoow=fvELBK8UP11XvgvoJoMk1JEqNSoVRDQGr27-6-0GEw@mail.gmail.com> <446372806.461162.1679778542118@office.mailbox.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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