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

The following snippet uses AWS SDK for JavaScript v3. For more information about how to install the client and usage, see Timestream Write Client - AWS SDK for JavaScript v3.

Also see Class CreateDatabaseCommand and CreateDatabase.

import { TimestreamWriteClient, CreateDatabaseCommand } from "@aws-sdk/client-timestream-write"; const writeClient = new TimestreamWriteClient({ region: "us-east-1" }); const params = { DatabaseName: "testDbFromNode" }; const command = new CreateDatabaseCommand(params); try { const data = await writeClient.send(command); console.log(`Database ${data.Database.DatabaseName} created successfully`); } catch (error) { if (error.code === 'ConflictException') { console.log(`Database ${params.DatabaseName} already exists. Skipping creation.`); } else { console.log("Error creating database", error); } }

The following snippet uses the AWS SDK for JavaScript V2 style. It is based on the sample application at Node.js sample Amazon Timestream application on GitHub.

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()); } }