Use the AWS CLI to recreate an AWS Glue database and its tables
Renaming a AWS 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 AWS CLI is configured for JSON output.
To change the default output format in the AWS CLI, run aws configure
.
To copy an AWS Glue Database using the AWS CLI
-
At a command prompt, run the following AWS CLI command to retrieve the definition of the AWS 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. -
Save the JSON output to a file with the name of the new database (for example,
) to your desktop.new_database_name
.json -
Open the
file in a text editor.new_database_name
.json -
In the JSON file, perform the following steps:
-
Remove the outer
{ "Database":
entry and the corresponding closing brace}
at the end of the file. -
Change the
Name
entry to the new database name. -
Remove the
CatalogId
field.
-
-
Save the file.
-
At a command prompt, run the following AWS 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 AWS CLI parameters from a file, see Loading AWS CLI parameters from a file in the AWS Command Line Interface User Guide. -
To verify that the new database has been created in AWS 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 AWS Glue table using the AWS CLI
-
At a command prompt, run the following AWS CLI command.
aws glue get-table --database-name
database_name
--nametable_name
For more information about the
get-table
command, see get-table. -
Save the JSON output to a file with the name of the table (for example,
table_name
.json) to your Windows desktop. -
Open the file in a text editor.
-
In the JSON file, remove the outer
{"Table":
entry and the corresponding closing brace}
at the end of the file. -
In the JSON file, remove the following entries and their values:
-
DatabaseName
– This entry is not required because thecreate-table
CLI command uses the--database-name
parameter. -
CreateTime
-
UpdateTime
-
CreatedBy
-
IsRegisteredWithLakeFormation
-
CatalogId
-
VersionId
-
-
Save the table definition file.
-
At a command prompt, run the following AWS 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 AWS Glue and can be queried from Athena.
-
Repeat the steps to copy each additional table to the new database in AWS Glue.