testcontainers/testcontainers-java · error · SQLException
Error while executing init script
Error message
Error while executing init script: {} What it means
When the init script is found and read but ScriptUtils.executeDatabaseScript fails with a ScriptException (bad SQL), runInitScriptIfRequired wraps it in this SQLException. The SQL in the init script itself is invalid or cannot be executed against the database.
Solutions
- Run the script manually against the same DB version to find the failing statement
- Fix SQL syntax / remove dialect-incompatible statements
- Check the chained cause (getCause()) for the exact line and statement that failed
- Split complex scripts; ensure statements are semicolon-terminated and comments are standard
Example fix
// before -- init.sql CREATE TABLE users(id INT; // after -- init.sql CREATE TABLE users(id INT PRIMARY KEY);
Defensive patterns
Strategy: try-catch
Try / catch
try {
runTestWithContainer();
} catch (SQLException e) {
if (e.getCause() instanceof javax.script.ScriptException) {
log.error("Init script failed: {}", e.getCause().getMessage(), e.getCause());
}
throw e;
} Prevention
- Test init scripts manually against the same DB version before wiring them in
- Keep scripts dialect-specific to the container's engine
- Make scripts idempotent and use IF EXISTS clauses
When it happens
Trigger: TC_INITSCRIPT (or withInitScript) SQL contains syntax errors, unsupported statements, script comment/separator mis-parsing, or fails mid-execution against the target database engine.
Common situations: Dialect-specific SQL (e.g. MySQL-specific syntax against Postgres); missing semicolons; stored procedures without proper delimiter handling; schema already exists from a prior run.
Understand the failure class
Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.
Related errors
- Could not load classpath init script
- Could not load classpath init script
- Failed to execute SQL script statement at line
- Could not load classpath init script
- Could not load classpath init script
AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12).
Data as JSON: /api/errors/887cdf6ec83ba9bc.
Report an issue: GitHub.
Appendix: source
Thrown at modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java:231
} else {
//classpath resource
resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
}
if (resource == null) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
throw new SQLException(
"Could not load classpath init script: " + initScriptPath + ". Resource not found."
);
}
String sql = IOUtils.toString(resource, StandardCharsets.UTF_8);
ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql);
} catch (IOException e) {
LOGGER.warn("Could not load classpath init script: {}", initScriptPath);
throw new SQLException("Could not load classpath init script: " + initScriptPath, e);
} catch (ScriptException e) {
LOGGER.error("Error while executing init script: {}", initScriptPath, e);
throw new SQLException("Error while executing init script: " + initScriptPath, e);
}
}
}
/**
* Run an init function (must be a public static method on an accessible class).
*
* @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url with r init function declarations.
* @param connection JDBC connection to apply init functions to.
* @throws SQLException on script or DB error
*/
private void runInitFunctionIfRequired(final ConnectionUrl connectionUrl, Connection connection)
throws SQLException {
if (connectionUrl.getInitFunction().isPresent()) {
String className = connectionUrl.getInitFunction().get().getClassName();
String methodName = connectionUrl.getInitFunction().get().getMethodName();
try {View on GitHub (pinned to 8e549514e3)