{"record":{"id":"8f3bc2a8774ad287","repo":"apache/beam","slug":"database-s-already-exists","errorCode":null,"errorMessage":"Database '%s' already exists.","messagePattern":"Database '(.+?)' already exists\\.","errorType":"validation","errorClass":"SqlUtil.newContextException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/CatalogSchema.java","lineNumber":114,"sourceCode":"        if (catalog.createDatabase(name)) {\n          LOG.info(\"Successfully created database '{}'\", name);\n        } else {\n          alreadyExists = true;\n        }\n      } catch (Exception e) {\n        throw SqlUtil.newContextException(\n            databaseIdentifier.getParserPosition(),\n            RESOURCE.internal(\n                format(\"Encountered an error when creating database '%s': %s\", name, e)));\n      }\n    }\n\n    if (alreadyExists) {\n      String message = format(\"Database '%s' already exists.\", name);\n      if (ifNotExists || name.equals(DEFAULT)) {\n        LOG.info(\"Database '{}' already exists.\", name);\n      } else {\n        throw SqlUtil.newContextException(\n            databaseIdentifier.getParserPosition(), RESOURCE.internal(message));\n      }\n    }\n\n    subSchemas.put(name, new BeamCalciteSchema(name, connection, catalog.metaStore(name)));\n  }\n\n  public void useDatabase(SqlIdentifier identifier) {\n    String name = SqlDdlNodes.name(identifier);\n    if (!subSchemas.containsKey(name)) {\n      if (!catalog.databaseExists(name)) {\n        throw SqlUtil.newContextException(\n            identifier.getParserPosition(),\n            RESOURCE.internal(String.format(\"Cannot use database: '%s' not found.\", name)));\n      }\n      subSchemas.put(name, new BeamCalciteSchema(name, connection, catalog.metaStore(name)));\n    }\n","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/CatalogSchema.java#L96-L132","documentation":"Thrown by CatalogSchema.createDatabase when the database already exists in the catalog and the statement did not include IF NOT EXISTS and the name is not the default database. Calcite wraps it with the statement's parser position.","triggerScenarios":"Executing `CREATE DATABASE name` (without IF NOT EXISTS) when subSchemas/catalog already contains `name`, and name != DEFAULT.","commonSituations":"Re-running an idempotent setup/seed SQL script, a migration script executed twice, or CI re-applying DDL against a persistent catalog.","solutions":["Add IF NOT EXISTS to the CREATE DATABASE statement.","Drop the existing database first if recreation is intended (DROP DATABASE ... ).","Use the built-in 'default' database instead of creating a duplicate.","Guard the script to only create the database when absent."],"exampleFix":"// before\nCREATE DATABASE analytics;\n// after\nCREATE DATABASE IF NOT EXISTS analytics;","handlingStrategy":"validation","validationCode":"// Java: skip DDL if the database already exists\nif (!catalog.databaseExists(name)) {\n  stmt.execute(\"CREATE DATABASE \" + name);\n}","typeGuard":null,"tryCatchPattern":"// Java\ncatch (SQLException e) {\n  if (e.getMessage().matches(\"(?s).*Database '.*' already exists\\\\.\")) {\n    LOG.info(\"database already present; continuing\");\n  } else throw e;\n}","preventionTips":["Always use CREATE DATABASE IF NOT EXISTS in setup scripts.","Make migration/seed scripts idempotent.","Check catalog.databaseExists() before issuing DDL.","Avoid recreating the built-in default database."],"tags":["sql","ddl","database","duplicate"],"backgroundTag":"file-already-exists","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}