You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TestContainers PostgreSQLContainer中设置max_prepared_transactions?

Can I set the 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:39:01