pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Wienhold <ewie@ewie.name>
To: Markus Pilman <markus@pilman.ch>
To: pgsql-general@postgresql.org
Subject: Re: Binding Postgres to port 0 for testing
Date: Sat, 25 Mar 2023 19:10:19 +0100 (CET)
Message-ID: <1804501070.456751.1679767819698@office.mailbox.org> (raw)
In-Reply-To: <CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com>
References: <CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com>

> On 25/03/2023 18:01 CET Markus Pilman <markus@pilman.ch> 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 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)
>
> 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.  It
tries every port number starting from 5432.

[0] https://manpages.debian.org/testing/postgresql-common/pg_virtualenv.1.en.html

--
Erik





view thread (8+ messages)  latest in thread

Message-ID: <1804501070.456751.1679767819698@office.mailbox.org>
Permalink:  ../1804501070.456751.1679767819698@office.mailbox.org/
Also on:    postgresql.org/message-id/1804501070.456751.1679767819698@office.mailbox.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ewie@ewie.name, markus@pilman.ch
  Subject: Re: Binding Postgres to port 0 for testing
  In-Reply-To: <1804501070.456751.1679767819698@office.mailbox.org>
  References: <CALavoowMtHgaX_pVhtkVuynYcjM8k7cF4DxxLzcp7ih7JEoRAw@mail.gmail.com> <1804501070.456751.1679767819698@office.mailbox.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox