Skip to content

Commit a35d699

Browse files
Use a healthcheck which runs a select 1 query
Turns out if you try to connect to PG as soon as `pg_isready` exits 0, you fail to connect. Let's change the healthcheck to run an actual query. We need to compute this later as we need credentials.
1 parent bf7c3ea commit a35d699

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

packages/modules/postgresql/src/postgresql-container.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ export class PostgreSqlContainer extends GenericContainer {
99

1010
constructor(image = "postgres:13.3-alpine") {
1111
super(image);
12-
this.withExposedPorts(POSTGRES_PORT)
13-
.withHealthCheck({
14-
test: ["CMD-SHELL", "pg_isready -U postgres"],
15-
interval: 10000,
16-
timeout: 5000,
17-
retries: 3,
18-
})
19-
.withWaitStrategy(Wait.forHealthCheck())
20-
.withStartupTimeout(120_000);
12+
this.withExposedPorts(POSTGRES_PORT).withStartupTimeout(120_000);
2113
}
2214

2315
public withDatabase(database: string): this {
@@ -41,6 +33,13 @@ export class PostgreSqlContainer extends GenericContainer {
4133
POSTGRES_USER: this.username,
4234
POSTGRES_PASSWORD: this.password,
4335
});
36+
this.withHealthCheck({
37+
test: ["CMD-SHELL", `PGPASSWORD=${this.password} psql -U ${this.username} -d ${this.database} -c 'SELECT 1;'`],
38+
interval: 100,
39+
timeout: 100,
40+
retries: 1000,
41+
});
42+
this.withWaitStrategy(Wait.forHealthCheck());
4443
return new StartedPostgreSqlContainer(await super.start(), this.database, this.username, this.password);
4544
}
4645
}

0 commit comments

Comments
 (0)