Using the Amazon CLI to recreate an Amazon Glue database and its tables - Amazon Athena
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using the Amazon CLI to recreate an Amazon Glue database and its tables

Renaming a Amazon Glue database directly is not possible, but you can copy its definition, modify the definition, and use the definition to recreate the database with a different name. Similarly, you can copy the definitions of the tables in the old database, modify the definitions, and use the modified definitions to recreate the tables in the new database.

Note

The method presented does not copy table partitioning.

The following procedure for Windows assumes that your Amazon CLI is configured for JSON output. To change the default output format in the Amazon CLI, run aws configure.

To copy an Amazon Glue Database using the Amazon CLI
  1. At a command prompt, run the following Amazon CLI command to retrieve the definition of the Amazon Glue database that you want to copy.

    aws glue get-database --name database_name

    For more information about the get-database command, see get-database.

  2. Save the JSON output to a file with the name of the new database (for example, new_database_name.json) to your desktop.

  3. Open the new_database_name.json file in a text editor.

  4. In the JSON file, change the Name entry to the new database name.

  5. Remove the CatalogId field.

  6. Save the file.

  7. At a command prompt, run the following Amazon CLI command to use the modified database definition file to create the database with the new name.

    aws glue create-database --database-input "file://~/Desktop\new_database_name.json"

    For more information about the create-database command, see create-database. For information about loading Amazon CLI parameters from a file, see Loading Amazon CLI parameters from a file in the Amazon Command Line Interface User Guide.

  8. To verify that the new database has been created in Amazon Glue, run the following command:

    aws glue get-database --name new_database_name

Now you are ready to get the definition for a table that you want to copy to the new database, modify the definition, and use the modified definition to recreate the table in the new database. This procedure does not change the table name.

To copy an Amazon Glue table using the Amazon CLI
  1. At a command prompt, run the following Amazon CLI command.

    aws glue get-table --database-name database_name --name table_name

    For more information about the get-table command, see get-table.

  2. Save the JSON output to a file with the name of the table (for example, table_name.json) to your Windows desktop.

  3. Open the file in a text editor.

  4. In the JSON file, remove the outer {"Table": entry and the corresponding closing brace } at the end of the file.

  5. In the JSON file, remove the following entries and their values:

    • DatabaseName – This entry is not required because the create-table CLI command uses the --database-name parameter.

    • CreateTime

    • UpdateTime

    • CreatedBy

    • IsRegisteredWithLakeFormation

    • CatalogId

    • VersionId

  6. Save the table definition file.

  7. At a command prompt, run the following Amazon CLI command to recreate the table in the new database:

    aws glue create-table --database-name new_database_name --table-input "file://~/Desktop\table_name.json"

    For more information about the create-table command, see create-table.

    The table now appears in the new database in Amazon Glue and can be queried from Athena.

  8. Repeat the steps to copy each additional table to the new database in Amazon Glue.