postgres
postgres copied to clipboard
Skip postgres restart on first run
Hi, we are using docker images with testcontainers. Each time new container is created, the db is initialized, migration scripts are applied and postgres server is restarted, adding around 25-50% startup time of whole container. This multiplied by hundred containers adds unnecessarily few hundred seconds. I've conducted some tests with pre-initialized database and the startup time significantly dropped, but that's really tricky to be done.
Any chance we could get a optimization setting for tests, to skip this server restart?
See https://github.com/docker-library/postgres/issues/929#issuecomment-2071146443 for somewhere I was answering something else recently, but I think my explanation there is probably helpful for explaining why what you're asking for isn't something we can reasonably accomplish with stock PostgreSQL, specifically:
We can't run SQL commands against PostgreSQL (generally) unless it is running and we have an appropriate method of access, and we can't start up PostgreSQL as the container's "PID 1" and still run more code reliably (nor can we start it and then "graduate" that instance to be PID 1)
FWIW, now https://github.com/docker-library/postgres/pull/1150 means this is pretty easy to accomplish in a slightly more esoteric way -- use that script in a Kubernetes initContainer and then run PostgreSQL in your main pod and "Bob's your uncle"
(there's still a "restart" there, but it's a cleaner one at a higher platform level, not in a single container)
For greater speed with testcontainer, it would be much faster to run the migration in an image build and store that result in a registry
@arvenil here's an example: ~~https://github.com/LaurentGoderre/image-mount-demo/tree/main/db~~
https://github.com/LaurentGoderre/postgres-init-demo