Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable SQL logs in PostgreSQLContainer during tests

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.

like image 843
alexgbelov Avatar asked Dec 17 '25 21:12

alexgbelov


1 Answers

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.

like image 70
Panagiotis Bougioukos Avatar answered Dec 19 '25 10:12

Panagiotis Bougioukos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!