From 759880a800ae2c2d4fc25a5a4efa5ad4a6dd4f87 Mon Sep 17 00:00:00 2001 From: Mike Reeves Date: Fri, 17 Apr 2026 16:43:41 -0400 Subject: [PATCH] Wait for TCP-ready postgres, not the init-phase Unix socket docker-entrypoint.sh runs the init-scripts phase with listen_addresses='' (Unix socket only). The old pg_isready check passed there and then raced the docker_temp_server_stop shutdown before the final postgres started. pg_isready -h 127.0.0.1 only returns success once the real CMD binds TCP, so downstream psql execs never land during the shutdown window. --- salt/postgres/telegraf_users.sls | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/salt/postgres/telegraf_users.sls b/salt/postgres/telegraf_users.sls index 804065bae..4719d363a 100644 --- a/salt/postgres/telegraf_users.sls +++ b/salt/postgres/telegraf_users.sls @@ -11,19 +11,22 @@ {% if TG_OUT in ['POSTGRES', 'BOTH'] %} # docker_container.running returns as soon as the container starts, but on -# first-init docker-entrypoint.sh runs init scripts and then restarts -# postgres, so the next docker exec can hit "the database system is shutting -# down". Wait for pg_isready before any psql work. +# first-init docker-entrypoint.sh starts a temporary postgres with +# `listen_addresses=''` to run /docker-entrypoint-initdb.d scripts, then +# shuts it down before exec'ing the real CMD. A default pg_isready check +# (Unix socket) passes during that ephemeral phase and races the shutdown +# with "the database system is shutting down". Checking TCP readiness on +# 127.0.0.1 only succeeds after the final postgres binds the port. postgres_wait_ready: cmd.run: - name: | for i in $(seq 1 60); do - if docker exec so-postgres pg_isready -U postgres -q 2>/dev/null; then + if docker exec so-postgres pg_isready -h 127.0.0.1 -U postgres -q 2>/dev/null; then exit 0 fi sleep 2 done - echo "so-postgres did not become ready within 120s" >&2 + echo "so-postgres did not accept TCP connections within 120s" >&2 exit 1 - require: - docker_container: so-postgres