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 1pg8LW-0007WX-Pr for pgsql-general@arkaria.postgresql.org; Sat, 25 Mar 2023 18:10:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pg8LV-0002tD-IZ for pgsql-general@arkaria.postgresql.org; Sat, 25 Mar 2023 18:10:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pg8LV-0002t4-7k for pgsql-general@lists.postgresql.org; Sat, 25 Mar 2023 18:10:29 +0000 Received: from mout-u-204.mailbox.org ([2001:67c:2050:101:465::204]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pg8LS-0004Bi-Cb for pgsql-general@postgresql.org; Sat, 25 Mar 2023 18:10:28 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (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-204.mailbox.org (Postfix) with ESMTPS id 4PkRvK0Flvz9sQC; Sat, 25 Mar 2023 19:10:21 +0100 (CET) Date: Sat, 25 Mar 2023 19:10:19 +0100 (CET) From: Erik Wienhold To: Markus Pilman , pgsql-general@postgresql.org Message-ID: <1804501070.456751.1679767819698@office.mailbox.org> In-Reply-To: References: Subject: Re: Binding Postgres to port 0 for testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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 18:01 CET Markus Pilman wrote: > > I am building a simple integration test framework for an application that= I > am building. For this project I am planning to use PostgreSQL. > > For testing purposes I'd like to always start with an empty database, > populate data, and, if the test was successful, delete everything. These > tests are single process tests. > > I'd like to run many processes in parallel and have one postgres server > process running for each. I realize that I could use one and use multiple > databases but I don't want to do this for a variety of reasons (one being > that I also want to test the control plane logic, the application is > multi-tenant and uses a database per tenant, having separate databases > simplifies debugging etc). > > Now the problem is that I need to find a TCP port for each running postgr= es > instance. There's multiple ways to do this, but by far the easiest one I > know is to bind to port 0. So my plan was to start postgres with "-p 0" a= nd > then parse stdout to figure out which port it actually uses. But that > doesn't seem to work: > > >postgres -D data/ -p 0 > =C2=A02023-03-25 16:39:54.271 GMT [13924] FATAL: 0 is outside the valid r= ange for parameter "port" (1 .. 65535) > > What would be the recommended way of addressing my issue? I would try to start Postgres with every port number in a for loop starting with port number 1024. The first one that works is your port number. And = you may not even have to parse stdout if you can pass that port number to your = tests. Maybe you can also use pg_virtualenv[0] from Debian's postgresql-common. I= t tries every port number starting from 5432. [0] https://manpages.debian.org/testing/postgresql-common/pg_virtualenv.1.e= n.html -- Erik