Create a keyspace (Go)
Creates a new keyspace in a database.
Keyspaces are used to store collections and tables in Astra DB Serverless.
|
You need an application token with permission to interact with the target database, such as the Database Administrator role. For more information, see Get endpoint and token. |
Result
Creates the specified keyspace.
A successful operation does not return anything.
Parameters
Use the CreateKeyspace method, which belongs to the AstraDatabaseAdmin type.
Method signature
func (a *AstraDatabaseAdmin) CreateKeyspace(
ctx context.Context,
keyspace string,
opts ...options.CreateKeyspaceOption
) error
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
The name of the keyspace to create. If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds. Keyspace names must follow these rules:
|
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. Whether to wait for the database to become active again before returning. If false, the method returns immediately after issuing the creation request, but you cannot work with the database until it becomes active again. Default: true |
|
Optional.
Controls how often to check the database status if Default: 10 seconds |
|
Optional.
Whether to set the working keyspace of the |
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to create a keyspace.
Create a keyspace
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get a database
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
// Get an admin object
admin, err := database.DatabaseAdmin()
if err != nil {
log.Fatal(err)
}
// Create a keyspace
err = admin.CreateKeyspace(ctx, "KEYSPACE_NAME")
if err != nil {
log.Fatal(err)
}
}
Create a keyspace and set it as the working keyspace for the database
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
)
func main() {
ctx := context.Background()
// Get a database
client := astra.NewClient()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
// Get an admin object
admin, err := database.DatabaseAdmin()
if err != nil {
log.Fatal(err)
}
// Create a keyspace
err = admin.CreateKeyspace(
ctx,
"KEYSPACE_NAME",
options.CreateKeyspace().SetUpdateDbKeyspace(true),
)
if err != nil {
log.Fatal(err)
}
}
Client reference
For more information, see the client reference.