Re: Binding Postgres to port 0 for testing - Mailing list pgsql-general

From Michael Paquier
Subject Re: Binding Postgres to port 0 for testing
Date
Msg-id ZCDxDqcuaK0FODCs@paquier.xyz
Whole thread Raw
In response to Binding Postgres to port 0 for testing  (Markus Pilman <markus@pilman.ch>)
Responses Re: Binding Postgres to port 0 for testing  (Markus Pilman <markus@pilman.ch>)
List pgsql-general
On Sat, Mar 25, 2023 at 11:01:33AM -0600, Markus Pilman wrote:
> Now the problem is that I need to find a TCP port for each running postgres
> 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" and
> then parse stdout to figure out which port it actually uses. But that
> doesn't seem to work:

Note that you can find some inspiration about that in the code tree
within src/test/perl/PostgreSQL/Test/Cluster.pm, particularly
get_free_port(), where we have now accumulated a couple of years of
experience in designing something that's rather safe, even if it comes
with its own limits.  It is in perl so perhaps you could just reuse it
rather than reinvent the wheel?  Of course, still it should not be
complicated to translate that in a different language, but there may
be no need to reinvent the wheel.  And seeing your first message with
the requirements you list, this does what you are looking for:
- Create an empty cluster.
- Freely create databases, tablespaces, queries, etc.
- Wipe out the whole.

The test cases around src/test/recovery/t/ could be a good starting
point, as well.
--
Michael

Attachment

pgsql-general by date:

Previous
From: Bryn Llewellyn
Date:
Subject: Re: Is the PL/pgSQL refcursor useful in a modern three-tier app?
Next
From: Markus Pilman
Date:
Subject: Re: Binding Postgres to port 0 for testing