From de3e5c4fec790bcd3145fdc85e57300c980842b7 Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (Pragmatismo)" Date: Thu, 5 Feb 2026 21:09:23 -0300 Subject: [PATCH] Fix PostgreSQL ready check for already-running instances Add pg_isready health check to the 'already running' branch to ensure PostgreSQL is properly detected as ready, even when running as a non-interactive user (sudo -u gbuser). This complements the previous fix for fresh PostgreSQL starts. --- src/core/bootstrap/mod.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/core/bootstrap/mod.rs b/src/core/bootstrap/mod.rs index 194b4bf9..ac8e05b2 100644 --- a/src/core/bootstrap/mod.rs +++ b/src/core/bootstrap/mod.rs @@ -501,6 +501,38 @@ impl BootstrapManager { Err(e) => { warn!("PostgreSQL might already be running: {}", e); + let mut ready = false; + for attempt in 1..=30 { + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + let status = SafeCommand::new("pg_isready") + .and_then(|c| { + c.args(&["-h", "localhost", "-p", "5432", "-d", "postgres"]) + }) + .ok() + .and_then(|cmd| cmd.execute().ok()) + .map(|o| o.status.success()) + .unwrap_or(false); + if status { + ready = true; + info!("PostgreSQL is ready (attempt {})", attempt); + break; + } + if attempt % 5 == 0 { + info!( + "Waiting for PostgreSQL to be ready... (attempt {}/30)", + attempt + ); + } + } + if !ready { + error!("PostgreSQL failed to become ready after 30 seconds"); + + let logs_dir = self.stack_dir("logs"); + dump_all_component_logs(&logs_dir); + + return Err(anyhow::anyhow!("PostgreSQL failed to start properly. Check logs above for details.")); + } + info!("Ensuring botserver database exists for already-running PostgreSQL..."); let db_password_from_vault = Self::get_db_password_from_vault().await; let db_password_from_env = std::env::var("BOOTSTRAP_DB_PASSWORD").ok();