googleapis/mcp-toolbox · error

invalid database_name %q: must match %s

Error message

invalid database_name %q: must match %s

What it means

ValidateDatabaseName enforces ^[A-Za-z_][A-Za-z0-9_-]{0,62}$ — a deliberately conservative identifier rule (must start with a letter or underscore, up to 63 chars, no quotes/semicolons/whitespace) even when the underlying engine would accept more. This is because the name is interpolated into DSNs and code-snippet templates across Postgres, MySQL, and SQL Server, where loose characters create injection or syntax hazards.

Source

Thrown at internal/util/cloudsqlconnect/inputvalidate.go:75

}

// ValidateGCEResourceName checks a VM name or zone name against the standard
// GCE resource-name rule (lowercase, digits, hyphen; must start with a letter
// and not end with a hyphen, max 63 chars).
func ValidateGCEResourceName(name, kind string) error {
	if !gceResourceRe.MatchString(name) {
		return fmt.Errorf("invalid %s %q: must match %s", kind, name, gceResourceRe)
	}
	return nil
}

// ValidateDatabaseName accepts the conservative subset of database identifier
// characters that's safe in DSNs and code-snippet templates across Postgres,
// MySQL and SQL Server. It deliberately rejects quotes, semicolons, and
// whitespace even when the engine itself would accept them.
func ValidateDatabaseName(name string) error {
	if !databaseNameRe.MatchString(name) {
		return fmt.Errorf("invalid database_name %q: must match %s", name, databaseNameRe)
	}
	return nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Rename or choose a database whose name is a plain identifier: letters, digits, underscores, hyphens, starting with a letter or underscore
  2. Strip or replace invalid characters (spaces, quotes, semicolons) in the supplied database_name
  3. If the existing database genuinely has a non-conforming name, create an alias database with a safe name and connect to that

Example fix

// before
ValidateDatabaseName("my db; --")
// after
ValidateDatabaseName("my_db")
Defensive patterns

Strategy: validation

Validate before calling

var databaseNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_-]{0,62}$`)
if !databaseNameRe.MatchString(dbName) {
    return fmt.Errorf("database_name %q must be <=63 chars, start with letter/underscore, contain only [A-Za-z0-9_-]", dbName)
}

Type guard

func isSafeDatabaseName(name string) bool {
    return regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_-]{0,62}$`).MatchString(name)
}

Try / catch

if err := cloudsqlconnect.ValidateDatabaseName(dbName); err != nil {
    return fmt.Errorf("refusing to connect: %w (quotes/semicolons/spaces are rejected for injection safety)", err)
}

Prevention

When it happens

Trigger: Passing a database_name containing spaces, quotes, semicolons, slashes, dots, Unicode, or a name longer than 63 characters, or one starting with a digit or hyphen.

Common situations: Database created with a quoted mixed-case/odd-character identifier (e.g. "my db" or "db-2024!"); names copied from URLs; names longer than 63 chars auto-generated by CI systems.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/7a1dead2dab89cc0. Report an issue: GitHub.