apache/iceberg · error · AlreadyExistsException

Cannot create namespace %s because it already exists in Glue

Error message

Cannot create namespace %s because it already exists in Glue

What it means

GlueCatalog.createNamespace throws AlreadyExistsException when Glue's CreateDatabase responds with AlreadyExistsException, i.e. a Glue database with the same name already exists. The library surfaces it because namespaces are unique within a catalog and creating a duplicate would silently overwrite nothing but signals a caller logic problem.

Source

Thrown at aws/src/main/java/org/apache/iceberg/aws/glue/GlueCatalog.java:478

      throw e;
    }

    LOG.info("Successfully renamed table from {} to {}", from, to);
  }

  @Override
  public void createNamespace(Namespace namespace, Map<String, String> metadata) {
    try {
      glue.createDatabase(
          CreateDatabaseRequest.builder()
              .catalogId(awsProperties.glueCatalogId())
              .databaseInput(
                  IcebergToGlueConverter.toDatabaseInput(
                      namespace, metadata, awsProperties.glueCatalogSkipNameValidation()))
              .build());
      LOG.info("Created namespace: {}", namespace);
    } catch (software.amazon.awssdk.services.glue.model.AlreadyExistsException e) {
      throw new AlreadyExistsException(
          "Cannot create namespace %s because it already exists in Glue", namespace);
    }
  }

  @Override
  public List<Namespace> listNamespaces(Namespace namespace) throws NoSuchNamespaceException {
    if (!namespace.isEmpty()) {
      // if it is not a list all op, just check if the namespace exists and return empty.
      if (namespaceExists(namespace)) {
        return Lists.newArrayList();
      }
      throw new NoSuchNamespaceException(
          "Glue does not support nested namespace, cannot list namespaces under %s", namespace);
    }

    // should be safe to list all before returning the list, instead of dynamically load the list.
    String nextToken = null;
    List<Namespace> results = Lists.newArrayList();

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Check `catalog.namespaceExists(namespace)` before calling createNamespace, or treat AlreadyExistsException as success in idempotent setup code.
  2. Use a create-if-not-exists pattern: catch AlreadyExistsException and continue instead of failing the job.
  3. Verify you are pointing at the intended Glue catalog ID/region — the name may already exist in the wrong environment.
  4. If the existing namespace has wrong metadata, drop and recreate it explicitly rather than relying on create.

Example fix

// before
catalog.createNamespace(Namespace.of("analytics"), props);

// after
Namespace ns = Namespace.of("analytics");
if (!catalog.namespaceExists(ns)) {
  catalog.createNamespace(ns, props);
}
Defensive patterns

Strategy: try-catch

Validate before calling

Namespace ns = Namespace.of("analytics");
if (catalog.namespaceExists(ns)) {
  LOG.info("Namespace already exists, skipping create");
}

Try / catch

try {
  catalog.createNamespace(ns, props);
} catch (AlreadyExistsException e) {
  LOG.info("Namespace {} already exists, treating as success", ns);
}

Prevention

When it happens

Trigger: Calling catalog.createNamespace(Namespace.of("db"), props) when Glue already contains a database named `db`; running idempotent setup scripts twice; two concurrent jobs both creating the same namespace.

Common situations: Initialization scripts that don't check existence first; re-running a failed pipeline from scratch; environment cloning where the namespace was created out-of-band in the AWS console; races between parallel deployments.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/5b2eec67ee718a23. Report an issue: GitHub.