Create a keyspace (Go)

Creates a new keyspace in a database.

Keyspaces are used to store collections and tables in Hyper-Converged Database (HCD).

Result

Creates the specified keyspace.

A successful operation does not return anything.

Parameters

Use the CreateKeyspace method, which belongs to the DataAPIDatabaseAdmin type.

Method signature
func (a *DataAPIDatabaseAdmin) CreateKeyspace(
  ctx context.Context,
  keyspace string,
  opts ...options.CreateKeyspaceOption
) error
Name Type Summary

ctx

context.Context

The context for the operation.

keyspace

string

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.

opts

…​options.CreateKeyspaceOption

Optional. A builder to generate options for this operation. See Methods of the CreateKeyspaceOption builder for more details.

Methods of the CreateKeyspaceOption builder
Method Summary

SetReplication(v map[string]any)

Optional. Specifies the options for data replication across database nodes.

For an example, see Create a keyspace and specify replication options.

SetUpdateDbKeyspace(v bool)

Optional. Whether to set the working keyspace of the Database or AsyncDatabase object that created the current DataAPIDatabaseAdmin object to the new keyspace.

UpdateAPIOptions(v …​APIOption)

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(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			),
	)

	// 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(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			),
	)

	// 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)
	}
}

Create a keyspace and specify replication options

For more information about data replication, see Data replication.

Example using SimpleStrategy:

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(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			),
	)

	// Get an admin object
	admin, err := database.DatabaseAdmin()

	if err != nil {
		log.Fatal(err)
	}

	// Create a keyspace
	err = admin.CreateKeyspace(
		ctx,
		"temp3",
		options.CreateKeyspace().
			SetReplication(map[string]any{
				"class":             "SimpleStrategy",
				"ReplicationFactor": 3,
			}),
	)

	if err != nil {
		log.Fatal(err)
	}
}

Example using NetworkTopologyStrategy:

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(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			),
	)

	// 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().
			SetReplication(map[string]any{
				"class":       "NetworkTopologyStrategy",
				"datacenter1": 3,
				"datacenter2": 2,
			}),
	)

	if err != nil {
		log.Fatal(err)
	}
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM