@@ -3,6 +3,7 @@ package launch
33import (
44 "context"
55 "fmt"
6+ "net/url"
67 "time"
78
89 "github.com/avast/retry-go/v4"
@@ -398,9 +399,22 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID
398399 state .Plan .AppName , appOrgSlug , clusterID , clusterOrgSlug )
399400 }
400401
402+ // Build connection URI with the database name from the plan (if provided)
403+ connectionUri := cluster .Credentials .ConnectionUri
404+ dbName := state .Plan .Postgres .ManagedPostgres .DbName
405+ if dbName != "" {
406+ // Parse the base connection URI and replace the database name
407+ parsedUri , err := url .Parse (cluster .Credentials .ConnectionUri )
408+ if err != nil {
409+ return fmt .Errorf ("failed to parse connection URI: %w" , err )
410+ }
411+ parsedUri .Path = "/" + dbName
412+ connectionUri = parsedUri .String ()
413+ }
414+
401415 // Set the connection string as a secret
402416 secrets := map [string ]string {
403- "DATABASE_URL" : cluster . Credentials . ConnectionUri ,
417+ "DATABASE_URL" : connectionUri ,
404418 }
405419
406420 flapsClient := flapsutil .ClientFromContext (ctx )
@@ -409,7 +423,7 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID
409423 }
410424
411425 fmt .Fprintf (io .Out , "Managed Postgres cluster %s is now attached to %s\n " , clusterID , state .Plan .AppName )
412- fmt .Fprintf (io .Out , "The following secret was added to %s:\n DATABASE_URL=%s\n " , state .Plan .AppName , cluster . Credentials . ConnectionUri )
426+ fmt .Fprintf (io .Out , "The following secret was added to %s:\n DATABASE_URL=%s\n " , state .Plan .AppName , connectionUri )
413427
414428 return nil
415429}
0 commit comments