Hi, is it possible to give postgresql testcontainer a custom postgresql.conf file? Or add custom settings to postgres?
I have included maven dependency
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.10.6</version>
</dependency>
And using 'Database containers launched via JDBC URL scheme' for DB url As such have the setting in my Spring Boot app as:
datasource:
url: jdbc:tc:postgresql:10-alpine:///databasename
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
I need to have a custom setting in postgresql.conf. Is there a way of pushing postgresql.conf to the docker container started by testcontainers?
Have asked the same question here:
https://stackoverflow.com/questions/54766846/how-do-you-include-postgresql-conf-on-docker-container-when-using-org-testcontai
Thank you in advance
Melissa
Just ran into the same problem. I need to have a custom setting in postgresql.conf for the postgres docker container.
Any updates on this?
@ameenhere no updates from myself, still having the same problem.
I also needed this, I ended up building my own docker containers with postgresql in order to be able to control this, it's quite easy to do, you can look here to see the source of my containers: https://github.com/pgjdbc/pgadba/tree/master/src/test/resources
And then I used them like this:
public static PostgreSQLContainer getNewWithTls() {
PostgreSQLContainer container = new PostgreSQLContainer("capitol/postgresql-tls:debian-stretch-postgresql10");
container.withTmpFs(singletonMap("/var/lib/postgresql/data", "rw"));
container.start();
return container;
}
@alexanderkjall How did you manage to customize the container that is picked up when using "Database containers launched via JDBC URL scheme" ?
I would say that using volumns via "Database containers launched via JDBC URL scheme" (to customise a container) has not been implemented yet?
Is anyone able to verfy this?
Using standard docker commands you can supply your own Postgres server configuration via 'Use a custom config file' for example:
docker run -d --name postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf -e POSTGRES_PASSWORD=postgres postgres:10-alpine -c 'config_file=/etc/postgresql/postgresql.conf'
However testcontainers 'Database containers launched via JDBC URL scheme' currently only allows for TC_INITSCRIPT or TC_INITFUNCTION.
Looking at @alexanderkjall example he's use the
org.testcontainers.containers.GenericContainer.withTmpFs function
Further investigation shows me that
org.testcontainers.containers.MariaDBContainer might be using a way to pass in custom config to container via;
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d", "mariadb-default-conf");
optionallyMapResourceParameterAsVolume is a function of abstract class JdbcDatabaseContainer which PostgreSQLContainer is also extending.
PostgreSQLContainer does not use optionallyMapResourceParameterAsVolume from what I can see.
From what I can see its the org.testcontainers.jdbc.ContainerDatabaseDriver which drives which DB container is started for 'Database containers launched via JDBC URL scheme'
And to sort out: possibly an additional db url query param such as TC_INITSCRIPT or TC_INITFUNCTION would be needed.
Which can then do the volume mapping for us
@melissapalmer I assume you are correct. But I have found a workaround to my problem. We were using Database container objects for our integration tests and wanted to customize a postgres config (namely max_locks_per_transaction). Eventually ended up extending some of the testcontainer classes and modifying the command used to start the container, from "postgres" to "postgres -c config=value -c config2=value2 ..." as required. Here is the source code of what I did, https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/container/DhisPostgreSQLContainer.java
I feel you may also be able to extend org.testcontainers.jdbc.ContainerDatabaseDriver into using such customized Database container objects?
@kiview
Does the underlying problem I have on this issue relate to #1239 possibly?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.
This issue has been automatically closed due to inactivity. We apologise if this is still an active problem for you, and would ask you to re-open the issue if this is the case.
@ameenhere Based on your comment this worked for me:
@Rule
public PostgreSQLContainer container = new PostgreSQLContainer<>()
.withCommand("postgres -c max_prepared_transactions=10");
No need for a custom subclass.
Most helpful comment
Just ran into the same problem. I need to have a custom setting in postgresql.conf for the postgres docker container.
Any updates on this?