{"record":{"id":"887cdf6ec83ba9bc","repo":"testcontainers/testcontainers-java","slug":"error-while-executing-init-script","errorCode":null,"errorMessage":"Error while executing init script: {}","messagePattern":"Error while executing init script: (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java","lineNumber":231,"sourceCode":"                } else {\n                    //classpath resource\n                    resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);\n                }\n                if (resource == null) {\n                    LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                    throw new SQLException(\n                        \"Could not load classpath init script: \" + initScriptPath + \". Resource not found.\"\n                    );\n                }\n\n                String sql = IOUtils.toString(resource, StandardCharsets.UTF_8);\n                ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql);\n            } catch (IOException e) {\n                LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                throw new SQLException(\"Could not load classpath init script: \" + initScriptPath, e);\n            } catch (ScriptException e) {\n                LOGGER.error(\"Error while executing init script: {}\", initScriptPath, e);\n                throw new SQLException(\"Error while executing init script: \" + initScriptPath, e);\n            }\n        }\n    }\n\n    /**\n     * Run an init function (must be a public static method on an accessible class).\n     *\n     * @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url with r init function declarations.\n     * @param connection    JDBC connection to apply init functions to.\n     * @throws SQLException on script or DB error\n     */\n    private void runInitFunctionIfRequired(final ConnectionUrl connectionUrl, Connection connection)\n        throws SQLException {\n        if (connectionUrl.getInitFunction().isPresent()) {\n            String className = connectionUrl.getInitFunction().get().getClassName();\n            String methodName = connectionUrl.getInitFunction().get().getMethodName();\n\n            try {","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/jdbc/src/main/java/org/testcontainers/jdbc/ContainerDatabaseDriver.java#L213-L249","documentation":"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.","triggerScenarios":"TC_INITSCRIPT (or withInitScript) SQL contains syntax errors, unsupported statements, script comment/separator mis-parsing, or fails mid-execution against the target database engine.","commonSituations":"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.","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"],"exampleFix":"// before\n-- init.sql\nCREATE TABLE users(id INT;\n// after\n-- init.sql\nCREATE TABLE users(id INT PRIMARY KEY);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    runTestWithContainer();\n} catch (SQLException e) {\n    if (e.getCause() instanceof javax.script.ScriptException) {\n        log.error(\"Init script failed: {}\", e.getCause().getMessage(), e.getCause());\n    }\n    throw e;\n}","preventionTips":["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"],"tags":["jdbc","testcontainers","sql","init-script"],"backgroundTag":"sql-query-failed","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}