{"record":{"id":"aca6b6133ba87c3c","repo":"testcontainers/testcontainers-java","slug":"error-while-executing-init-script-initscriptpath-scriptutils","errorCode":null,"errorMessage":"Error while executing init script: ${initScriptPath}","messagePattern":"Error while executing init script: (.+?)","errorType":"exception","errorClass":"UncategorizedScriptException","httpStatus":null,"severity":"error","filePath":"modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java","lineNumber":219,"sourceCode":"        try {\n            URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);\n            if (resource == null) {\n                resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath);\n                if (resource == null) {\n                    LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n                    throw new ScriptLoadException(\n                        \"Could not load classpath init script: \" + initScriptPath + \". Resource not found.\"\n                    );\n                }\n            }\n            String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8);\n            executeDatabaseScript(databaseDelegate, initScriptPath, scripts);\n        } catch (IOException e) {\n            LOGGER.warn(\"Could not load classpath init script: {}\", initScriptPath);\n            throw new ScriptLoadException(\"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 UncategorizedScriptException(\"Error while executing init script: \" + initScriptPath, e);\n        }\n    }\n\n    public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script)\n        throws ScriptException {\n        executeDatabaseScript(\n            databaseDelegate,\n            scriptPath,\n            script,\n            false,\n            false,\n            DEFAULT_COMMENT_PREFIX,\n            DEFAULT_STATEMENT_SEPARATOR,\n            DEFAULT_BLOCK_COMMENT_START_DELIMITER,\n            DEFAULT_BLOCK_COMMENT_END_DELIMITER\n        );\n    }\n","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java#L201-L237","documentation":"ScriptUtils.runInitScript caught a ScriptException thrown while executing the init script and rethrows it as UncategorizedScriptException with this message. This means the script loaded fine but failed during execution against the database (e.g. SQL error inside executeDatabaseScript).","triggerScenarios":"executeDatabaseScript throwing ScriptException due to SQL syntax errors, duplicate table creation, constraint violations, or connection failures while running initScriptPath statements.","commonSituations":"Scripts written for one database dialect run against another; re-running non-idempotent DDL on a reused container; referencing tables/columns that don't exist yet.","solutions":["Inspect the nested UncategorizedScriptException cause for the actual SQL error and line number.","Fix the SQL in the init script (syntax, dialect compatibility, ordering).","Make scripts idempotent (CREATE TABLE IF NOT EXISTS) when containers are reused across tests.","Test the script directly against the target database version."],"exampleFix":"// before (init.sql)\nCREATE TABLE users (id INT PRIMARY KEY); // rerun -> already exists\n// after (init.sql)\nCREATE TABLE IF NOT EXISTS users (id INT PRIMARY KEY);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    ScriptUtils.runInitScript(delegate, initScriptPath);\n} catch (UncategorizedScriptException e) {\n    if (e.getMessage().startsWith(\"Error while executing init script\")) {\n        Throwable sql = e.getCause(); // real SQL failure with details\n        LOG.error(\"Init script failed: {}\", sql.getMessage());\n    }\n    throw e;\n}","preventionTips":["Write idempotent DDL (IF NOT EXISTS) for reusable containers.","Test scripts against the exact database image/version used in tests.","Keep dialect-specific scripts separate per database module."],"tags":["sql","script-execution","init-script","database"],"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"}