Binding Postgres to port 0 for testing

From: Markus Pilman <markus(at)pilman(dot)ch>
To: pgsql-general(at)postgresql(dot)org
Subject: Binding Postgres to port 0 for testing
Date: 2023-03-25 17:01:33
Message-ID: CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

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 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:

>postgres -D data/ -p 0
2023-03-25 16:39:54.271 GMT [13924] FATAL: 0 is outside the valid range
for parameter "port" (1 .. 65535)

Is there a reason this is not allowed? What would be the recommended way of
addressing my issue?

Best Markus

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thorsten Glaser 2023-03-25 18:03:00 Re: Binding Postgres to port 0 for testing
Previous Message Achilleas Mantzios 2023-03-25 15:44:06 Re: PostgreSQL vs MariaDB