1515
1616from testcontainers .core .generic import DbContainer
1717from testcontainers .core .utils import raise_for_deprecated_parameter
18- from testcontainers .core .waiting_utils import wait_container_is_ready
18+ from testcontainers .core .wait_strategies import RunFunctionWaitStrategy
19+ from testcontainers .core .waiting_utils import WaitStrategyTarget
1920
2021_UNSET = object ()
2122
@@ -64,6 +65,7 @@ def __init__(
6465 self .driver = f"+{ driver } " if driver else ""
6566
6667 self .with_exposed_ports (self .port )
68+ self .waiting_for (RunFunctionWaitStrategy (_check_postgres_ready ))
6769
6870 def _configure (self ) -> None :
6971 self .with_env ("POSTGRES_USER" , self .username )
@@ -87,7 +89,6 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =
8789 port = self .port ,
8890 )
8991
90- @wait_container_is_ready ()
9192 def _connect (self ) -> None :
9293 escaped_single_password = self .password .replace ("'" , "'\" '\" '" )
9394 result = self .exec (
@@ -99,3 +100,11 @@ def _connect(self) -> None:
99100 )
100101 if result .exit_code :
101102 raise ConnectionError ("pg_isready is not ready yet" )
103+
104+
105+ def _check_postgres_ready (container : WaitStrategyTarget ) -> bool :
106+ if not isinstance (container , PostgresContainer ):
107+ raise AssertionError ("This check can only wait for postgres containers to start up" )
108+ # This raises a ConnectionError if not ready yet
109+ container ._connect ()
110+ return True
0 commit comments