InvalidParameterValue: DBName must begin with a letter and contain only alphanumeric characters

InvalidParameterValue: DBName must begin with a letter and contain only alphanumeric characters

TL;DR: Don't use hyphen (-) for DBName.

And if you want to understand the reasons behind that, keep reading further.

You are creating an RDS instance with Terraform.

You have coded the instance, executed terraform plan and all looks great!

That's great!
Now you can apply the changes!

So you run terraform apply, Terraform starts calling AWS API and suddenly you get an error:

InvalidParameterValue: DBName must begin with a letter and contain only alphanumeric characters

But why?
You wonder!

And I also wonder!

I have seen this a couple of times already...
But I always forget why this happens...
Mostly I solve this issue once at the beginning of setting up naming convention in the organization and then I forget about this issue...

Now recently I have also forgotten about that, but I was asked by my teammate to help them with the issue.

After a couple of minutes I have finally got it!

We were deploying Postgres!

The problem here is more complex than just AWS or Terraform constraints.
Terraform is supposed to manage AWS resources and AWS provides many different services, some of them are well known open source technologies that can have their own limitations.

Postgres doesn't allow to use hyphens (-) in the database name!
Only underscores (_)!

By the Postgres docs:

SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($). Note that dollar signs are not allowed in identifiers according to the letter of the SQL standard, so their use might render applications less portable.

But you didn't set up Postgres?

Apparently MySQL doesn't seem to support hyphens too:

  • Permitted characters in unquoted identifiers:

    • ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore)

    • Extended: U+0080 .. U+FFFF

And MS SQL Server too:

The first character must be one of the following:

  • A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin characters from a through z, from A through Z, and also letter characters from other languages.

  • The underscore (_), at sign (@), or number sign (#).

I'm neither databases nor SQL expert, but looks like hyphen is not allowed as database name in the most popular SQL engines.

I cannot confirm that for sure, as I cannot find any sort of official SQL Language reference manual in the Internet, but it seems like it is a limitation of SQL for identifiers and database name is an identifier.
Vendors of SQL engines can follow the SQL standard more or less strictly, so there may be engines that could work with hyphens (-) in database name, but this would be an exception to what I have seen in past and during research for this post.

If, by any chance, you know more on that matter, feel free to share your thoughts in a comment!
I'm always happy to hear from you and learn from you!