docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

Allow init script to fail and prevent DB startup (for testing) #1113

Closed nyurik closed 8 months ago

nyurik commented 1 year ago

In an automated testing environment, it is often needed to pre-load a db with some data. This is usually done by a script, e.g. /docker-entrypoint-initdb.d/10_init.sh

The script would then run psql -e -P pager=off -v ON_ERROR_STOP=1 -f "some_sql_file.sql" - but the problem is that there is no documented way to prevent database from starting up in case an error occurs, even if the script has set -euo pipefail at the beginning.

Is there an established way to do this? Thanks!

Gruummy commented 1 year ago

There is no way for the init scipt to "fail" and prevent start up of postgres. When it fails then container would exit and container engine would restart it. ... depends on how you manage the container.

What you can do is to patch the init script in a way ... if the psql command fails ... not to end.

I would write a special hint file like "/postgres-init-interuption" (or whatever name for the file) in the filesystem (in your error case) which could be checked by some alerting and for analysis if it happens unintended

... and then start to "loop" and wait for 10 seconds in the loop as long this hint file exists in the file system.

If it happens ... the error case you described .... you can then shell manually into the container and do your investigations / fixes / whatever you have in mind and simply remove then the hint file and the startup would continue.

But this will not work when you run it in kubernetes .. there then additional challenges will exist about readyness and livenessprobs ... and so on.

In this theorie i imply that the permissions of the created file is adjusted after creation that it can be removed from a shell process started by you manually ... and a few other things.

I hope you can understand the idea behind this.

But in general such an approach is in containerized world a no go.

Containers can ... but should not ... be operated like the older bare metal operations approach.

tianon commented 9 months ago

See also https://github.com/docker-library/postgres/issues/159, which might actually be closer to what you're looking for than #1150 is.