nixos/postgresql: make postgresql.target wait until recovery is done

The new postgresql.target will now wait until recovery is done and
read/write connections are possible.

This allows ensure* scripts and downstream migrations to work properly
after recovery from backup.

Resolves #346886
This commit is contained in:
Wolfgang Walther
2025-05-09 14:36:01 +02:00
parent 41c5662cbe
commit 9656e1aa9d
4 changed files with 26 additions and 3 deletions

View File

@@ -879,7 +879,15 @@ in
# Wait for PostgreSQL to be ready to accept connections.
script =
''
while ! psql -d postgres -c "" 2> /dev/null; do
check-connection() {
psql -d postgres -v ON_ERROR_STOP=1 <<-' EOF'
SELECT pg_is_in_recovery() \gset
\if :pg_is_in_recovery
\i still-recovering
\endif
EOF
}
while ! check-connection 2> /dev/null; do
if ! systemctl is-active --quiet postgresql.service; then exit 1; fi
sleep 0.1
done