Create database - Amazon Timestream

Create database

You can use the following code snippets to create a database.

Note

These code snippets are based on full sample applications on GitHub. For more information about how to get started with the sample applications, see Sample application.

Java
public void createDatabase() { System.out.println("Creating database"); CreateDatabaseRequest request = new CreateDatabaseRequest(); request.setDatabaseName(DATABASE_NAME); try { amazonTimestreamWrite.createDatabase(request); System.out.println("Database [" + DATABASE_NAME + "] created successfully"); } catch (ConflictException e) { System.out.println("Database [" + DATABASE_NAME + "] exists. Skipping database creation"); } }
Java v2
public void createDatabase() { System.out.println("Creating database"); CreateDatabaseRequest request = CreateDatabaseRequest.builder().databaseName(DATABASE_NAME).build(); try { timestreamWriteClient.createDatabase(request); System.out.println("Database [" + DATABASE_NAME + "] created successfully"); } catch (ConflictException e) { System.out.println("Database [" + DATABASE_NAME + "] exists. Skipping database creation"); } }
Go
// Create database. createDatabaseInput := &timestreamwrite.CreateDatabaseInput{ DatabaseName: aws.String(*databaseName), } _, err = writeSvc.CreateDatabase(createDatabaseInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Database successfully created") } fmt.Println("Describing the database, hit enter to continue")
Python
def create_database(self): print("Creating Database") try: self.client.create_database(DatabaseName=Constant.DATABASE_NAME) print("Database [%s] created successfully." % Constant.DATABASE_NAME) except self.client.exceptions.ConflictException: print("Database [%s] exists. Skipping database creation" % Constant.DATABASE_NAME) except Exception as err: print("Create database failed:", err)
Node.js
async function createDatabase() { console.log("Creating Database"); const params = { DatabaseName: constants.DATABASE_NAME }; const promise = writeClient.createDatabase(params).promise(); await promise.then( (data) => { console.log(`Database ${data.Database.DatabaseName} created successfully`); }, (err) => { if (err.code === 'ConflictException') { console.log(`Database ${params.DatabaseName} already exists. Skipping creation.`); } else { console.log("Error creating database", err); } } ); }
.NET
public async Task CreateDatabase() { Console.WriteLine("Creating Database"); try { var createDatabaseRequest = new CreateDatabaseRequest { DatabaseName = Constants.DATABASE_NAME }; CreateDatabaseResponse response = await writeClient.CreateDatabaseAsync(createDatabaseRequest); Console.WriteLine($"Database {Constants.DATABASE_NAME} created"); } catch (ConflictException) { Console.WriteLine("Database already exists."); } catch (Exception e) { Console.WriteLine("Create database failed:" + e.ToString()); } }