diff --git a/internal/command/launch/launch_databases.go b/internal/command/launch/launch_databases.go index ea54adb87d..b4b99e60fc 100644 --- a/internal/command/launch/launch_databases.go +++ b/internal/command/launch/launch_databases.go @@ -3,6 +3,7 @@ package launch import ( "context" "fmt" + "net/url" "time" "github.com/avast/retry-go/v4" @@ -398,9 +399,22 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID state.Plan.AppName, appOrgSlug, clusterID, clusterOrgSlug) } + // Build connection URI with the database name from the plan (if provided) + connectionUri := cluster.Credentials.ConnectionUri + dbName := state.Plan.Postgres.ManagedPostgres.DbName + if dbName != "" { + // Parse the base connection URI and replace the database name + parsedUri, err := url.Parse(cluster.Credentials.ConnectionUri) + if err != nil { + return fmt.Errorf("failed to parse connection URI: %w", err) + } + parsedUri.Path = "/" + dbName + connectionUri = parsedUri.String() + } + // Set the connection string as a secret secrets := map[string]string{ - "DATABASE_URL": cluster.Credentials.ConnectionUri, + "DATABASE_URL": connectionUri, } flapsClient := flapsutil.ClientFromContext(ctx) @@ -409,7 +423,7 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID } fmt.Fprintf(io.Out, "Managed Postgres cluster %s is now attached to %s\n", clusterID, state.Plan.AppName) - fmt.Fprintf(io.Out, "The following secret was added to %s:\n DATABASE_URL=%s\n", state.Plan.AppName, cluster.Credentials.ConnectionUri) + fmt.Fprintf(io.Out, "The following secret was added to %s:\n DATABASE_URL=%s\n", state.Plan.AppName, connectionUri) return nil } diff --git a/internal/command/launch/webui.go b/internal/command/launch/webui.go index ffdeafe379..170b14587d 100644 --- a/internal/command/launch/webui.go +++ b/internal/command/launch/webui.go @@ -123,8 +123,11 @@ func (state *launchState) EditInWebUi(ctx context.Context) error { } // Apply settings from the form + // Check both "db_name" (Go struct json tag) and "name" (API/UI convention) if dbName, ok := mpgData["db_name"].(string); ok && dbName != "" { state.Plan.Postgres.ManagedPostgres.DbName = dbName + } else if dbName, ok := mpgData["name"].(string); ok && dbName != "" { + state.Plan.Postgres.ManagedPostgres.DbName = dbName } if plan, ok := mpgData["plan"].(string); ok && plan != "" { state.Plan.Postgres.ManagedPostgres.Plan = plan