Description
Someone asked how to use this extension with testcontainers a while back and I had a very long-winded answer (but with sample code!)... then realized I was an idiot and there's a much easier answer that can be added to the wiki.
Using a pre-configured Docker image
When I'm using my custom docker images I use
PostgreSQLContainer getContainer() {
DockerImageName imageName = DockerImageName.parse("beargiles/postgres-pljava:15.4").asCompatibleSubstituteFor("postgres");
PostgreSQLContainer db = new PostgreSQLContainer(ImageName);
// db.withInitScript("/path/to/initScript");
db.withImagePullPolicy((s) -> false);
return db;
}
The key is the asCompatibleSubstituteFor()
call.
The initialization script can load the jars you require, perform any required SQL calls, etc.
Note: I strongly recommend that any initialization scripts be limited to preparing the test data. Any custom
extensions (UDF, UDT, etc.) should be set up using a SQL Deployment Descriptor bundled in your jar(s). This ensures the SQL extensions and your java implementation remain in sync.
You may not need the withImagePullPolicy()
call - I might only need it since I'm developing the docker images locally.
Using an official PostgreSQL image
The short answer is that you can't. The initialization scripts can't install system packages.
The long answer is that you could - but it would be difficult. You would need to install pl/java using the Container#withCommand(String)
command once the database container is running. That's a multistep process
even if you're taking advantage of the existing Debian packages available on the official PostgreSQL image.
If you're feeling bold look at the first two targets in this Dockerfile.