faokunega / pg-embed

Run a Postgresql database locally on Linux, MacOS or Windows as part of another Rust application or test.
MIT License
116 stars 26 forks source link

How to bind an ephemeral post for the db? #31

Open capveg-netdebug opened 11 months ago

capveg-netdebug commented 11 months ago

Hello,

Thank you for making this crate - super useful!

I'm running automated tests back-to-back where each test is binding its own PgEmbed instance but they are all binding the same port (the default from the example; 5432). I'd like to be able to bind an ephemeral port that the OS allocates to avoid other race conditions, but naively passing '0' doesn't seem to work. Has anyone else looked into this or has a work around?

I could obviously bind port = 1024 + rand(), but that's just a different, less often race condition waiting to happen which I would like to avoid.

Thanks in advance!

EDIT: Apparently postgres itself won't accept an ephemeral port, so I guess there's not going to be a way to do this with PgEmbed :-(

capveg@LAPTOP-O7RS71IJ:~/.cache/pg-embed/linux/amd64/13.6.0/bin$ ./postgres -p 0
2023-12-18 20:42:37.735 GMT [574351] FATAL:  0 is outside the valid range for parameter "port" (1 .. 65535)

But I can't be the only one with the problem of back-to-back tests not being able to re-use the same port. What do other people do?