Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgB8T-00073x-5w for pgsql-general@arkaria.postgresql.org; Sat, 25 Mar 2023 21:09:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pgB8S-0000TG-2x for pgsql-general@arkaria.postgresql.org; Sat, 25 Mar 2023 21:09:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgB8R-0000N2-MH for pgsql-general@lists.postgresql.org; Sat, 25 Mar 2023 21:09:11 +0000 Received: from mout-u-107.mailbox.org ([2001:67c:2050:101:465::107]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgB8P-0003RM-2i for pgsql-general@postgresql.org; Sat, 25 Mar 2023 21:09:10 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4PkWsV4pjjz9sQL; Sat, 25 Mar 2023 22:09:02 +0100 (CET) Date: Sat, 25 Mar 2023 22:09:02 +0100 (CET) From: Erik Wienhold To: Markus Pilman Cc: pgsql-general@postgresql.org Message-ID: <446372806.461162.1679778542118@office.mailbox.org> In-Reply-To: References: <1804501070.456751.1679767819698@office.mailbox.org> Subject: Re: Binding Postgres to port 0 for testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 25/03/2023 20:10 CET Markus Pilman 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