|  | 
| 11 | 11 | #    License for the specific language governing permissions and limitations | 
| 12 | 12 | #    under the License. | 
| 13 | 13 | import os | 
| 14 |  | -from time import sleep | 
| 15 | 14 | from typing import Optional | 
| 16 | 15 | 
 | 
| 17 |  | -from testcontainers.core.config import testcontainers_config as c | 
| 18 | 16 | from testcontainers.core.generic import DbContainer | 
| 19 | 17 | from testcontainers.core.utils import raise_for_deprecated_parameter | 
| 20 |  | -from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs | 
|  | 18 | +from testcontainers.core.waiting_utils import wait_container_is_ready | 
| 21 | 19 | 
 | 
| 22 | 20 | _UNSET = object() | 
| 23 | 21 | 
 | 
| @@ -91,45 +89,13 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] = | 
| 91 | 89 | 
 | 
| 92 | 90 |     @wait_container_is_ready() | 
| 93 | 91 |     def _connect(self) -> None: | 
| 94 |  | -        # postgres itself logs these messages to the standard error stream: | 
| 95 |  | -        # | 
| 96 |  | -        # $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 \ | 
| 97 |  | -        # > | grep -o -a -m 1 -h 'database system is ready to accept connections' | 
| 98 |  | -        # 2024-08-03 00:13:02.799 EDT [70226] LOG:  starting PostgreSQL 14.11 (Homebrew) .... | 
| 99 |  | -        # 2024-08-03 00:13:02.804 EDT [70226] LOG:  listening on IPv4 address "127.0.0.1", port 5432 | 
| 100 |  | -        # ... | 
| 101 |  | -        # ^C2024-08-03 00:13:04.226 EDT [70226] LOG:  received fast shutdown request | 
| 102 |  | -        # ... | 
| 103 |  | -        # | 
| 104 |  | -        # $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 2>&1 \ | 
| 105 |  | -        # > | grep -o -a -m 1 -h 'database system is ready to accept connections' | 
| 106 |  | -        # database system is ready to accept connections | 
| 107 |  | -        # | 
| 108 |  | -        # and the setup script inside docker library postgres | 
| 109 |  | -        # uses pg_ctl: | 
| 110 |  | -        # https://github.com/docker-library/postgres/blob/66da3846b40396249936938ee17e9684e6968a57/16/alpine3.20/docker-entrypoint.sh#L261-L282 | 
| 111 |  | -        # which prints logs to stdout: | 
| 112 |  | -        # https://www.postgresql.org/docs/current/app-pg-ctl.html#:~:text=the%20server%27s%20standard%20output%20and%20standard%20error%20are%20sent%20to%20pg_ctl%27s%20standard%20output | 
| 113 |  | -        # | 
| 114 |  | -        # so we must wait for both the setup and real startup: | 
| 115 |  | -        predicate_streams_and = True | 
| 116 |  | - | 
| 117 |  | -        wait_for_logs( | 
| 118 |  | -            self, | 
| 119 |  | -            ".*database system is ready to accept connections.*", | 
| 120 |  | -            c.max_tries, | 
| 121 |  | -            c.sleep_time, | 
| 122 |  | -            predicate_streams_and=predicate_streams_and, | 
| 123 |  | -            # | 
|  | 92 | +        escaped_single_password = self.password.replace("'", "'\"'\"'") | 
|  | 93 | +        result = self.exec( | 
|  | 94 | +            [ | 
|  | 95 | +                "sh", | 
|  | 96 | +                "-c", | 
|  | 97 | +                f"PGPASSWORD='{escaped_single_password}' psql --username {self.username} --dbname {self.dbname} --host 127.0.0.1 -c 'select version();'", | 
|  | 98 | +            ] | 
| 124 | 99 |         ) | 
| 125 |  | - | 
| 126 |  | -        count = 0 | 
| 127 |  | -        while count < c.max_tries: | 
| 128 |  | -            status, _ = self.exec(f"pg_isready -hlocalhost -p{self.port} -U{self.username}") | 
| 129 |  | -            if status == 0: | 
| 130 |  | -                return | 
| 131 |  | - | 
| 132 |  | -            sleep(c.sleep_time) | 
| 133 |  | -            count += 1 | 
| 134 |  | - | 
| 135 |  | -        raise RuntimeError("Postgres could not get into a ready state") | 
|  | 100 | +        if result.exit_code: | 
|  | 101 | +            raise ConnectionError("pg_isready is not ready yet") | 
0 commit comments