AddDatabase() databases are not created when deploying to Kubernetes
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
## Summary
When using `AddDatabase()` with Postgres, MySQL, or SQL Server and deploying to Kubernetes via `aspire deploy`, the named database is never created. The API service gets a connection string referencing a non-existent database, causing connection failures.
## Root Cause
All three database technologies create databases via SQL commands during Aspire orchestration:
- **PostgreSQL** (`PostgresBuilderExtensions.cs:88-94`): `ResourceReadyEvent` → `CREATE DATABASE`
- **MySQL** (`MySqlBuilderExtensions.cs:71-74`): `ResourceReadyEvent` → `CREATE DATABASE IF NOT EXISTS`
- **SQL Server** (`SqlServerBuilderExtensions.cs:87-90`): `OnResourceReady` → `CREATE DATABASE`
In Kubernetes deployment mode, the Aspire orchestrator is not running — containers are deployed via Helm charts. The `ResourceReadyEvent` callbacks that create the databases never fire.
## Expected Behavior
When deploying to Kubernetes, databases registered via `AddDatabase()` should be created. Possible approaches:
1. Set appropriate container env vars (`POSTGRES_DB`, `MYSQL_DATABASE`)
2. Generate Kubernetes init containers with SQL scripts
3. Generate Kubernetes Jobs that run after the database container is ready
## Affected Technologies
- PostgreSQL (`AddPostgres().AddDatabase()`)
- MySQL (`AddMySql().AddDatabase()`)
- SQL Server (`AddSqlServer().AddDatabase()`)
**Note:** MongoDB is NOT affected because it creates databases on first use.
## Workaround
Reference the server directly without `AddDatabase()` and use the default database.
Discovered during K8s deploy E2E testing.
Contributor guide
Research direction
Start with the ResourceReadyEvent or OnResourceReady paths in PostgresBuilderExtensions.cs, MySqlBuilderExtensions.cs, and SqlServerBuilderExtensions.cs, then trace how aspire deploy generates Kubernetes resources. Decide which deployment mechanism fits the existing design, and verify that AddDatabase() creates the named database and allows the API service to connect for all three technologies.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, kubernetes, mysql, postgresql, sql
- Domain
- databases, devops, infrastructure
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100