{"record":{"id":"0fe31501c72e4710","repo":"NationalSecurityAgency/ghidra","slug":"attempted-to-drop-non-bsim-database","errorCode":null,"errorMessage":"attempted to drop non-BSim database","messagePattern":"attempted to drop non-BSim database","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/PostgresFunctionDatabase.java","lineNumber":288,"sourceCode":"\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Connect to database and examine schema\n\t\t\tHashSet<String> tableNames = new HashSet<>();\n\t\t\tpostgresDs.initializeFrom(defaultDs);\n\t\t\ttry (Connection c = initConnection(); Statement st = c.createStatement()) {\n\t\t\t\ttry (ResultSet rs = st.executeQuery(\n\t\t\t\t\t\"SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name\")) {\n\t\t\t\t\twhile (rs.next()) {\n\t\t\t\t\t\ttableNames.add(rs.getString(1));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Spot check for a few BSim table names that always exist\n\t\t\tif (!tableNames.contains(\"keyvaluetable\") || !tableNames.contains(\"desctable\") ||\n\t\t\t\t!tableNames.contains(\"weighttable\")) {\n\t\t\t\tthrow new SQLException(\"attempted to drop non-BSim database\");\n\t\t\t}\n\n\t\t\tpostgresDs.dispose(); // disconnect before dropping database\n\n\t\t\tMsg.info(this, \"Dropping BSim postgresql database: \" + serverInfo);\n\t\t\tsb = new StringBuilder(\"DROP DATABASE \");\n\t\t\tUtils.escapeIdentifier(sb, serverInfo.getDBName());\n\t\t\tdefaultSt.executeUpdate(sb.toString());\n\t\t}\n\t\tfinally {\n\t\t\t// ensure \n\t\t\tpostgresDs.initializeFrom(defaultDs);\n\t\t}\n\t}\n\n\t/**\n\t * \n\t * @throws SQLException if there is a problem creating or executing the query","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/NationalSecurityAgency/ghidra/blob/d5f144c24d6bc53c9cbf4448c6d11143e7696206/Ghidra/Features/BSim/src/main/java/ghidra/features/bsim/query/client/PostgresFunctionDatabase.java#L270-L306","documentation":"Thrown by PostgresFunctionDatabase.dropDatabase() as a safety guard. Before issuing DROP DATABASE, the code queries the database schema and spot-checks for three core BSim tables that always exist in a valid BSim database: keyvaluetable, desctable, and weighttable. If any is missing, the drop is refused to prevent accidental destruction of a non-BSim database.","triggerScenarios":"Calling dropDatabase() on a PostgreSQL database that does not contain all three required BSim core tables. This happens when the target database was never properly initialized as a BSim database, was partially created, or is a completely unrelated database that happens to be targeted by misconfiguration.","commonSituations":"The server info / connection string points at the wrong database (e.g. a default 'postgres' database or a user's personal database). A previous createDatabase failed midway, leaving an incomplete schema. The database name was mistyped in configuration.","solutions":["Verify the BSimServerInfo database name is correct and points to the intended BSim database.","If the database is partially created from a failed createDatabase, manually drop it via psql before retrying.","Confirm keyvaluetable, desctable, and weighttable all exist in the target database before attempting the programmatic drop."],"exampleFix":"// before — wrong db name in config\nBSimServerInfo info = new BSimServerInfo(DBType.postgres, ...\n    \"postgres\"); // default db, not a BSim db\ndb.dropDatabase(); // throws \"attempted to drop non-BSim database\"\n\n// after — correct BSim db name\nBSimServerInfo info = new BSimServerInfo(DBType.postgres, ...\n    \"my_bsim_db\"); // contains keyvaluetable etc.\ndb.dropDatabase();","handlingStrategy":"validation","validationCode":"// Verify the target database is a valid BSim database before dropping\nSet<String> required = Set.of(\"keyvaluetable\", \"desctable\", \"weighttable\");\ntry (Connection c = dbConnection;\n     Statement st = c.createStatement();\n     ResultSet rs = st.executeQuery(\n         \"SELECT table_name FROM information_schema.tables WHERE table_schema='public'\")) {\n    Set<String> tables = new HashSet<>();\n    while (rs.next()) tables.add(rs.getString(1));\n    if (!tables.containsAll(required)) {\n        throw new IllegalStateException(\"Not a BSim database; aborting drop\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    db.dropDatabase();\n} catch (SQLException e) {\n    if (e.getMessage().contains(\"non-BSim database\")) {\n        // wrong database; verify the BSimServerInfo db name and correct config\n        throw e;\n    }\n    throw e;\n}","preventionTips":["Double-check the database name in BSimServerInfo configuration.","Never point dropDatabase at the default 'postgres' database or unrelated databases.","If a prior createDatabase partially failed, clean up manually before retrying."],"tags":["bsim","ghidra","postgresql","database","safety-guard","misconfiguration"],"backgroundTag":null,"analyzedSha":"d5f144c24d6bc53c9cbf4448c6d11143e7696206","analyzedAt":"2026-08-14T01:00:57.564Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}