如何在TestContainers PostgreSQLContainer中设置max_prepared_transactions?
max_prepared_transactions parameter in TestContainers' PostgreSQLContainer? Yes, absolutely! You can easily configure the max_prepared_transactions parameter for a TestContainers PostgreSQLContainer—here are the most straightforward ways to do it:
1. Pass the parameter directly via the container's start command
PostgreSQL allows overriding configuration values through command-line arguments when starting the server. You can use TestContainers' withCommand method to append this parameter:
PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>(DockerImageName.parse("postgres:15-alpine")) .withCommand("postgres -c max_prepared_transactions=100");
Replace 100 with the value that fits your test needs. A quick note: if your tests rely heavily on prepared transactions, you'll want this value to be at least as large as max_connections (the default for that is usually 100), but for most testing scenarios, a value between 50-200 works perfectly.
2. Verify the setting is applied
To make sure the parameter took effect, you can run a quick SQL query once the container is up:
SHOW max_prepared_transactions;
This should return the exact value you set, confirming the configuration is active.
Bonus: Multiple configuration parameters
If you need to set several PostgreSQL settings at once, you can chain multiple -c flags in the command:
PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>(DockerImageName.parse("postgres:15-alpine")) .withCommand("postgres -c max_prepared_transactions=100 -c work_mem=64MB");
That's all there is to it—no need for custom config files or complex setups for most use cases!
内容的提问来源于stack exchange,提问作者Stefano

