googleapis/mcp-toolbox · error
error creating database: %w
Error message
error creating database: %w
What it means
Thrown in CreateDatabase when the sqladmin Databases.Insert call (projects.instances.databases.insert) fails. The create-database request was sent to the Cloud SQL Admin API and returned an error.
Source
Thrown at internal/sources/cloudsqladmin/cloud_sql_admin.go:195
}
return resp, nil
}
func (s *Source) CreateDatabase(ctx context.Context, name, project, instance, accessToken string) (any, error) {
database := sqladmin.Database{
Name: name,
Project: project,
Instance: instance,
}
service, err := s.GetService(ctx, accessToken)
if err != nil {
return nil, err
}
resp, err := service.Databases.Insert(project, instance, &database).Do()
if err != nil {
return nil, fmt.Errorf("error creating database: %w", err)
}
return resp, nil
}
func (s *Source) CreateUsers(ctx context.Context, project, instance, name, password string, iamUser bool, accessToken string) (any, error) {
service, err := s.GetService(ctx, accessToken)
if err != nil {
return nil, err
}
user := sqladmin.User{
Name: name,
}
if iamUser {
user.Type = "CLOUD_IAM_USER"
} else {
user.Type = "BUILT_IN"View on GitHub (pinned to 8cc6e09de2)
Solutions
- Check the database does not already exist (use the list_databases tool first).
- Use a valid identifier: lowercase letters, numbers, hyphens/underscores, starts with a letter, <= 64 chars.
- Grant roles/cloudsql.admin (cloudsql.databases.create) to the calling principal.
- Read the wrapped googleapi.Error (409 = already exists, 403 = permission, 404 = instance not found) to target the fix.
- Verify the target instance exists and is in RUNNABLE state.
Example fix
// before CreateDatabase(ctx, "MyDB", project, instance, token) // invalid chars + uppercase // after CreateDatabase(ctx, "mydb", project, instance, token)
Defensive patterns
Strategy: validation
Validate before calling
var dbNameRe = regexp.MustCompile(`^[a-z][a-z0-9_-]{0,63}$`)
func validDBName(name string) error {
if !dbNameRe.MatchString(name) {
return fmt.Errorf("invalid database name %q: must match [a-z][a-z0-9_-]* and be <= 64 chars", name)
}
if slices.Contains([]string{"postgres", "template0", "template1"}, name) {
return fmt.Errorf("database name %q is reserved", name)
}
return nil
} Type guard
func validDBNameBool(name string) bool {
return regexp.MustCompile(`^[a-z][a-z0-9_-]{0,63}$`).MatchString(name)
} Try / catch
out, err := src.CreateDatabase(ctx, name, project, instance, token)
if err != nil {
var gerr *googleapi.Error
if errors.As(err, &gerr) && gerr.Code == http.StatusConflict {
return nil // treat as already-exists / idempotent success
}
return fmt.Errorf("create database failed: %w", err)
} Prevention
- Validate names against [a-z][a-z0-9_-]* before calling.
- List existing databases first and skip creation if present (idempotent flows).
- Reserve names like 'postgres' on Postgres instances.
- Grant cloudsql.databases.create to the tool's service account.
When it happens
Trigger: Calling the create_database tool with a database name that already exists, an invalid name (bad characters/length), or when the caller lacks cloudsql.databases.create permission.
Common situations: Postgres reserves names like 'postgres' and names must match [a-z][a-z0-9_-]*; duplicate creation on retry after a partial failure; API not enabled in the project.
Related errors
- error cloning instance: %w
- error creating user: %w
- error getting instance: %w
- error listing databases: %w
- error listing instances: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/39167faf8e7d5147.
Report an issue: GitHub.