1

I have the following piece of code:

public static PostgreSQLContainer<?> postgreDBContainer = new PostgreSQLContainer<>("postgres:12")
        .withInitScript("init-database-test.sql")
        .withUsername("dba")
        .withPassword("dba");

Inside the init script, I'm creating some tablespaces and associating the folders:

CREATE TABLESPACE tsd01 OWNER dba LOCATION '/tsd01';
CREATE TABLESPACE tsi01 OWNER dba LOCATION '/tsi01';
CREATE TABLESPACE tsisecurity01 OWNER dba LOCATION '/tsisecurity01';

These folders for the tablespaces should be created before the init script runs. How do I can able to do that?

1 Answers1

1

I was able to solve this issue by extending the default PostgreSQLContainer and changing the containerIsStarted method:

public class CustomPostgreSQLContainer<SELF extends CustomPostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> {

    private static final Logger log = LoggerFactory.getLogger(CustomPostgreSQLContainer.class);

    public CustomPostgreSQLContainer() {
        super("postgres:12");
    }

    @Override
    protected void containerIsStarted(InspectContainerResponse containerInfo) {
        try {
            log.debug("M=containerIsStarted, creating database namespace folders and setting permissions");
            execInContainer("mkdir", "/tsd01");
            execInContainer("chown", "-R", "postgres.postgres", "/tsd01/");
            execInContainer("mkdir", "/tsi01");
            execInContainer("chown", "-R", "postgres.postgres", "/tsi01/");
            execInContainer("mkdir", "/tsisecurity01");
            execInContainer("chown", "-R", "postgres.postgres", "/tsisecurity01/");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        super.containerIsStarted(containerInfo);
    }
}