PostgreSQL PublishAsContainer + AddDatabase does not create database on deploy
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 201
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
When using `AddPostgres("postgres").PublishAsContainer().AddDatabase("clickdb")`, the PostgreSQL container deploys to Azure Container Apps with only the default `postgres` database. The named database `clickdb` is never created. During local `aspire run`, Aspire handles database creation automatically, but the deploy path (`aspire deploy`) does not replicate this behavior.
The dependent application (a Python FastAPI service) crashes on startup because the database doesn't exist.
### Expected Behavior
`aspire deploy` should ensure that databases declared via `AddDatabase()` are created in the PostgreSQL container on first deployment, matching the behavior of `aspire run`. Alternatively, the docs should clearly document that `PublishAsContainer()` does NOT auto-create named databases and that applications must handle database creation themselves.
### Steps To Reproduce
1. Create an apphost with:
```csharp
var clickdb = builder.AddPostgres("postgres")
.PublishAsContainer()
.AddDatabase("clickdb");
```
2. Create an application that references `clickdb` and connects using `ConnectionStrings__clickdb`
3. Run `aspire run` — works correctly, database is created
4. Run `aspire deploy` — PostgreSQL container starts but only has the default `postgres` database
5. The dependent application crashes with: `asyncpg.exceptions.InvalidCatalogNameError: database "clickdb" does not exist`
### Exceptions (if any)
```
asyncpg.exceptions.InvalidCatalogNameError: database "clickdb" does not exist
```
### .NET Version info
```
.NET SDK: 10.0.100
Runtime Environment:
OS Name: Mac OS X
OS Version: 26.3
OS Platform: Darwin
RID: osx-arm64
```
### Anything else?
**Aspire package versions:**
- `Aspire.AppHost.Sdk@13.3.0-pr.14149.g4bc5e9bf`
- `Aspire.Hosting.PostgreSQL@13.3.0-pr.14149.g4bc5e9bf`
- `Aspire.Hosting.Azure.AppContainers@13.3.0-pr.14149.g4bc5e9bf`
**IDE:** VS Code / Copilot CLI
**Workaround:** The application must connect to the default `postgres` database first, check if the target database exists via `pg_database`, create it if missing, then reconnect. This is non-trivial boilerplate that Aspire should handle.
Additionally, the `ConnectionStrings__clickdb` env var was empty in ACA (individual vars like `CLICKDB_HOST`, `CLICKDB_PORT` were populated), requiring additional fallback logic to construct the DSN from individual env vars.
Contributor guide
Research direction
Start with the apphost example using AddPostgres("postgres").PublishAsContainer().AddDatabase("clickdb"), then compare the resource behavior during aspire run and aspire deploy to Azure Container Apps. Trace the PostgreSQL deployment and connection-string handling, and reproduce the missing database and empty ConnectionStrings__clickdb variable. Done means deployment creates clickdb or the documented behavior and required configuration are clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp, fastapi, postgresql, python
- Domain
- backend, cloud, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100