> 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