From 2f4f05fd98b338c813b84e2722883a76b47b0388 Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Fri, 17 Oct 2025 19:42:04 +0200 Subject: [PATCH 1/6] fix: Support Postgres ready_conditions for already mounted volumes --- src/postgres/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index dd40a98..20c0d6b 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -125,8 +125,8 @@ impl Image for Postgres { fn ready_conditions(&self) -> Vec { vec![ - WaitFor::message_on_stderr("database system is ready to accept connections"), - WaitFor::message_on_stdout("database system is ready to accept connections"), + WaitFor::message_on_either_std("listening on IPv4 address \"0.0.0.0\", port 5432"), + WaitFor::message_on_either_std("database system is ready to accept connections"), ] } From acb12e9541efad96374c592f245e8a68424f38e6 Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Sat, 18 Oct 2025 08:43:35 +0200 Subject: [PATCH 2/6] fix: Use truncated listening log line --- src/postgres/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 20c0d6b..fb5d8a5 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -125,7 +125,7 @@ impl Image for Postgres { fn ready_conditions(&self) -> Vec { vec![ - WaitFor::message_on_either_std("listening on IPv4 address \"0.0.0.0\", port 5432"), + WaitFor::message_on_either_std("listening on IPv4 address"), WaitFor::message_on_either_std("database system is ready to accept connections"), ] } From f63cad0791e15840a58f2690e5a9ccbccbe622b6 Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Tue, 21 Oct 2025 09:52:55 +0200 Subject: [PATCH 3/6] fix: Use pg_isready to check for readiness --- src/postgres/mod.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index fb5d8a5..baed6f8 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, collections::HashMap}; -use testcontainers::{core::WaitFor, CopyDataSource, CopyToContainer, Image}; +use testcontainers::{core::{WaitFor, CmdWaitFor, ExecCommand, ContainerState}, CopyDataSource, CopyToContainer, Image}; const NAME: &str = "postgres"; const TAG: &str = "11-alpine"; @@ -124,12 +124,31 @@ impl Image for Postgres { } fn ready_conditions(&self) -> Vec { - vec![ - WaitFor::message_on_either_std("listening on IPv4 address"), - WaitFor::message_on_either_std("database system is ready to accept connections"), - ] + // actual wait for `ready_conditions` is be done in `exec_after_start` + vec![] } + fn exec_after_start( + &self, + cs: ContainerState, + ) -> Result, testcontainers::TestcontainersError> { + let mut commands = vec![]; + // with container running, we can use pg_isready to check if the db is ready to accept connections + let cmd = vec![ + "pg_isready".to_string(), + "--host".to_string(), + "localhost".to_string(), + self.env_vars.get("POSTGRES_DB").unwrap().to_string(), + "--username".to_string(), + self.env_vars.get("POSTGRES_USER").unwrap().to_string() + ]; + + commands.push(ExecCommand::new(cmd).with_cmd_ready_condition(CmdWaitFor::exit())); + + Ok(commands) + } + + fn env_vars( &self, ) -> impl IntoIterator>, impl Into>)> { From 59b26fa456c17e3377003c849ebe49d092e9303f Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Tue, 21 Oct 2025 09:55:38 +0200 Subject: [PATCH 4/6] nit --- src/postgres/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index baed6f8..9947882 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -148,7 +148,6 @@ impl Image for Postgres { Ok(commands) } - fn env_vars( &self, ) -> impl IntoIterator>, impl Into>)> { From 01a59a084ef8bc055e0106c17a7416fc19e6369a Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Tue, 21 Oct 2025 13:49:14 +0200 Subject: [PATCH 5/6] fix: Forgot dbname arg --- src/postgres/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 9947882..42ed4a2 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -138,6 +138,7 @@ impl Image for Postgres { "pg_isready".to_string(), "--host".to_string(), "localhost".to_string(), + "--dbname".to_string(), self.env_vars.get("POSTGRES_DB").unwrap().to_string(), "--username".to_string(), self.env_vars.get("POSTGRES_USER").unwrap().to_string() From 0b92a8cf81427edfc4f12c578d62d6dc8336ef83 Mon Sep 17 00:00:00 2001 From: Christian Brevik Date: Tue, 21 Oct 2025 14:12:59 +0200 Subject: [PATCH 6/6] fix: Hybrid strategy, wait for pg_isready to be ready --- src/postgres/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 42ed4a2..e8a1b1c 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -124,8 +124,9 @@ impl Image for Postgres { } fn ready_conditions(&self) -> Vec { - // actual wait for `ready_conditions` is be done in `exec_after_start` - vec![] + vec![ + WaitFor::message_on_either_std("database system is ready to accept connections"), + ] } fn exec_after_start( @@ -133,7 +134,6 @@ impl Image for Postgres { cs: ContainerState, ) -> Result, testcontainers::TestcontainersError> { let mut commands = vec![]; - // with container running, we can use pg_isready to check if the db is ready to accept connections let cmd = vec![ "pg_isready".to_string(), "--host".to_string(),