{"record":{"id":"39167faf8e7d5147","repo":"googleapis/mcp-toolbox","slug":"error-creating-database-w","errorCode":null,"errorMessage":"error creating database: %w","messagePattern":"error creating database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqladmin/cloud_sql_admin.go","lineNumber":195,"sourceCode":"\t}\n\treturn resp, nil\n}\n\nfunc (s *Source) CreateDatabase(ctx context.Context, name, project, instance, accessToken string) (any, error) {\n\tdatabase := sqladmin.Database{\n\t\tName:     name,\n\t\tProject:  project,\n\t\tInstance: instance,\n\t}\n\n\tservice, err := s.GetService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tresp, err := service.Databases.Insert(project, instance, &database).Do()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error creating database: %w\", err)\n\t}\n\treturn resp, nil\n}\n\nfunc (s *Source) CreateUsers(ctx context.Context, project, instance, name, password string, iamUser bool, accessToken string) (any, error) {\n\tservice, err := s.GetService(ctx, accessToken)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tuser := sqladmin.User{\n\t\tName: name,\n\t}\n\n\tif iamUser {\n\t\tuser.Type = \"CLOUD_IAM_USER\"\n\t} else {\n\t\tuser.Type = \"BUILT_IN\"","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqladmin/cloud_sql_admin.go#L177-L213","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nCreateDatabase(ctx, \"MyDB\", project, instance, token) // invalid chars + uppercase\n// after\nCreateDatabase(ctx, \"mydb\", project, instance, token)","handlingStrategy":"validation","validationCode":"var dbNameRe = regexp.MustCompile(`^[a-z][a-z0-9_-]{0,63}$`)\nfunc validDBName(name string) error {\n    if !dbNameRe.MatchString(name) {\n        return fmt.Errorf(\"invalid database name %q: must match [a-z][a-z0-9_-]* and be <= 64 chars\", name)\n    }\n    if slices.Contains([]string{\"postgres\", \"template0\", \"template1\"}, name) {\n        return fmt.Errorf(\"database name %q is reserved\", name)\n    }\n    return nil\n}","typeGuard":"func validDBNameBool(name string) bool {\n    return regexp.MustCompile(`^[a-z][a-z0-9_-]{0,63}$`).MatchString(name)\n}","tryCatchPattern":"out, err := src.CreateDatabase(ctx, name, project, instance, token)\nif err != nil {\n    var gerr *googleapi.Error\n    if errors.As(err, &gerr) && gerr.Code == http.StatusConflict {\n        return nil // treat as already-exists / idempotent success\n    }\n    return fmt.Errorf(\"create database failed: %w\", err)\n}","preventionTips":["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."],"tags":["gcp","cloud-sql","api-error","database"],"backgroundTag":"cloudsql-api-request-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}