I have a Java Spring application that uses a PostgreSQL docker container for the tests. I would like to be able to see the SQL logs in that PostgreSQL container, but they do not show up. Can you please tell me what I need to fix?
Test Container Configuration class:
@SuppressWarnings("PMD.TestClassWithoutTestCases")
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
@Bean
@ServiceConnection
PostgreSQLContainer<?> sharedDatabaseContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:17.2"))
.withDatabaseName("<redacted>").withUsername("<redacted>").withPassword("<redacted>")
.withCommand("postgres", "-c", "log_statement=all") // this seems to be ignored
.withExposedPorts(1234);
}
}
And here is how I am using that configuration in my test class:
@Import(TestcontainersConfiguration.class)
@SpringBootTest
@Tag("UnitTest")
public class RepositoryTest {
...
}
My tests run, but when I go to the logs for that testcontainer, I cannot see any SQL logs.
In some older test container versions, it was reported as an issue that withCommand was ignored during container startup.
I would suggest you try as a workaround instead of .withCommand the .withEnv("POSTGRES_INITDB_ARGS", "--log_statement=all").
Otherwise you can try updating your test libraries to a more recent version in which, this function is respected from the docker container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With