-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Module
PostgreSQL
Testcontainers version
Junit-Jupiter 1.20.4 and PostgreSQL 1.20.0
Using the latest Testcontainers version?
Yes
Host OS
macOS, Windows
Host Arch
x86, arm
Docker version
Client:
Version: 27.3.1
API version: 1.47
Go version: go1.22.7
Git commit: ce12230
Built: Fri Sep 20 11:38:18 2024
OS/Arch: darwin/arm64
Context: desktop-linux
Server: Docker Desktop 4.36.0 (175267)
Engine:
Version: 27.3.1
API version: 1.47 (minimum version 1.24)
Go version: go1.22.7
Git commit: 41ca978
Built: Fri Sep 20 11:41:19 2024
OS/Arch: linux/arm64
Experimental: false
containerd:
Version: 1.7.21
GitCommit: 472731909fa34bd7bc9c087e4c27943f9835f111
runc:
Version: 1.1.13
GitCommit: v1.1.13-0-g58aa920
docker-init:
Version: 0.19.0
GitCommit: de40ad0What happened?
I am currently trying out setting up our tests with a Postgres test container. For that I have created a PostgreSQLContainer like that:
@Container
private static final PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:14.15-alpine3.21")
.withUsername("postgres")
.withPassword("password")
.withCopyFileToContainer(MountableFile.forClasspathResource("schema.sql"), "/docker-entrypoint-initdb.d/schema.sql");The schema.sql file contains the schema required to run the tests.
Furthermore I have created a @BeforAll method to start the Postgres container and setup the HikariConfig:
@BeforeAll
public static void setup() {
postgresContainer.start();
hikariConfig = new HikariConfig();
hikariConfig.setDataSourceClassName("com.impossibl.postgres.jdbc.PGDataSource");
hikariConfig.setJdbcUrl(postgresContainer.getJdbcUrl().replaceFirst(":.*://", ":pgsql://"));
hikariConfig.addDataSourceProperty("user", postgresContainer.getUsername());
hikariConfig.addDataSourceProperty("password", postgresContainer.getPassword());
hikariConfig.addDataSourceProperty("serverName", postgresContainer.getHost());
hikariConfig.addDataSourceProperty("portNumber", postgresContainer.getFirstMappedPort());
hikariConfig.setLeakDetectionThreshold(10000);
hikariConfig.setMaximumPoolSize(10);
}Looking at the container logs it appears that the schema.sql file is executed:
waiting for server to start....2024-12-15 01:28:40.761 UTC [40] LOG: starting PostgreSQL 14.15 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit
2024-12-15 01:28:40.761 UTC [40] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-12-15 01:28:40.762 UTC [41] LOG: database system was shut down at 2024-12-15 01:28:40 UTC
2024-12-15 01:28:40.763 UTC [40] LOG: database system is ready to accept connections
done
server started
CREATE DATABASE
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/schema.sql
psql:/docker-entrypoint-initdb.d/schema.sql:1: NOTICE: table "players" does not exist, skipping
DROP TABLE
psql:/docker-entrypoint-initdb.d/schema.sql:2: NOTICE: table "players_2" does not exist, skipping
DROP TABLE
psql:/docker-entrypoint-initdb.d/schema.sql:3: NOTICE: table "backpacks" does not exist, skipping
DROP TABLE
psql:/docker-entrypoint-initdb.d/schema.sql:4: NOTICE: table "islands" does not exist, skipping
DROP TABLE
psql:/docker-entrypoint-initdb.d/schema.sql:5: NOTICE: table "home_locations" does not exist, skipping
DROP TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
waiting for server to shut down....2024-12-15 01:28:40.886 UTC [40] LOG: received fast shutdown request
2024-12-15 01:28:40.886 UTC [40] LOG: aborting any active transactions
2024-12-15 01:28:40.886 UTC [40] LOG: background worker "logical replication launcher" (PID 47) exited with exit code 1
2024-12-15 01:28:40.887 UTC [42] LOG: shutting down
2024-12-15 01:28:40.889 UTC [40] LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
2024-12-15 01:28:41.000 UTC [1] LOG: starting PostgreSQL 14.15 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit
2024-12-15 01:28:41.000 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-12-15 01:28:41.000 UTC [1] LOG: listening on IPv6 address "::", port 5432
2024-12-15 01:28:41.000 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-12-15 01:28:41.001 UTC [57] LOG: database system was shut down at 2024-12-15 01:28:40 UTC
2024-12-15 01:28:41.003 UTC [1] LOG: database system is ready to accept connections
Creating a breakpoint and looking at the database with IntelliJ also shows the created tables.

However as soon as I try to query the database it throws the following error:
java.lang.RuntimeException: com.impossibl.postgres.jdbc.PGSQLSimpleException: relation "public.players" does not exist
java.lang.RuntimeException: java.lang.RuntimeException: com.impossibl.postgres.jdbc.PGSQLSimpleException: relation "public.players" does not exist
at net.staticstudios.data.DataManager.loadAll(DataManager.java:93)
at net.staticstudios.data.PlayerTest.createPlayer(PlayerTest.java:59)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.RuntimeException: com.impossibl.postgres.jdbc.PGSQLSimpleException: relation "public.players" does not exist
at net.staticstudios.data.DataManager.loadAll(DataManager.java:83)
... 4 more
Caused by: com.impossibl.postgres.jdbc.PGSQLSimpleException: relation "public.players" does not exist
at com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException(ErrorUtils.java:197)
at com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException(ErrorUtils.java:174)
at com.impossibl.postgres.jdbc.ErrorUtils.makeSQLException(ErrorUtils.java:161)
at com.impossibl.postgres.jdbc.PGDirectConnection.execute(PGDirectConnection.java:514)
at com.impossibl.postgres.jdbc.PGPreparedStatement.lambda$parseIfNeeded$3(PGPreparedStatement.java:289)
at com.impossibl.postgres.jdbc.PGDirectConnection.getCachedPreparedStatement(PGDirectConnection.java:1608)
at com.impossibl.postgres.jdbc.PGPreparedStatement.parseIfNeeded(PGPreparedStatement.java:284)
at com.impossibl.postgres.jdbc.PGPreparedStatement.execute(PGPreparedStatement.java:329)
at com.impossibl.postgres.jdbc.PGPreparedStatement.executeQuery(PGPreparedStatement.java:357)
at com.impossibl.postgres.jdbc.PGPreparedStatement.executeQuery(PGPreparedStatement.java:106)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
at net.staticstudios.data.DataManager.loadUniqueData(DataManager.java:263)
at net.staticstudios.data.DataManager.loadAll(DataManager.java:79)
... 4 more
Relevant log output
Additional Information
While manually creating the schema in the @BeforeAll method works I would really like to know why exactly it is not possible like the way provided above.