Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3eadfb5

Browse files
Claudeclaude
andcommitted
fix: add support for individual PostgreSQL connection parameters
Fix bug that prevented individual PostgreSQL connection parameters from being shown in CLI help output and being used properly. The parameters were already defined in the DeploymentValues struct but weren't accessible due to a type mismatch when checking InMemoryDatabase value. This allows users to specify PostgreSQL connection details using individual environment variables, which is useful for systems with automatic password rotation. Closes #15264 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 30179ae commit 3eadfb5

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

cli/server.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,42 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
420420
config := r.createConfig()
421421

422422
builtinPostgres := false
423-
// Only use built-in if PostgreSQL URL isn't specified!
424-
if !vals.InMemoryDatabase && vals.PostgresURL == "" {
423+
424+
// Check if we have individual PostgreSQL connection parameters
425+
hasIndividualParams := len(vals.PostgresHost.String()) > 0 &&
426+
len(vals.PostgresUsername.String()) > 0 &&
427+
len(vals.PostgresPassword.String()) > 0 &&
428+
len(vals.PostgresDatabase.String()) > 0
429+
430+
// Build a connection URL from individual components if provided and no connection URL exists
431+
if !vals.InMemoryDatabase.Value() && vals.PostgresURL == "" && hasIndividualParams {
432+
port := vals.PostgresPort.String()
433+
if port == "" {
434+
port = "5432" // Default PostgreSQL port
435+
}
436+
437+
// Build the base connection string
438+
connURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s",
439+
vals.PostgresUsername.String(),
440+
vals.PostgresPassword.String(),
441+
vals.PostgresHost.String(),
442+
port,
443+
vals.PostgresDatabase.String())
444+
445+
// Add options if provided
446+
if len(vals.PostgresOptions.String()) > 0 {
447+
connURL = connURL + "?" + vals.PostgresOptions.String()
448+
}
449+
450+
logger.Debug(ctx, "using individual PostgreSQL connection parameters")
451+
err = vals.PostgresURL.Set(connURL)
452+
if err != nil {
453+
return xerrors.Errorf("set postgres url from components: %w", err)
454+
}
455+
}
456+
457+
// Only use built-in if PostgreSQL URL and individual parameters aren't specified!
458+
if !vals.InMemoryDatabase.Value() && vals.PostgresURL == "" && !hasIndividualParams {
425459
var closeFunc func() error
426460
cliui.Infof(inv.Stdout, "Using built-in PostgreSQL (%s)", config.PostgresPath())
427461
customPostgresCacheDir := ""
@@ -715,7 +749,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
715749
// nil, that case of the select will just never fire, but it's important not to have a
716750
// "bare" read on this channel.
717751
var pubsubWatchdogTimeout <-chan struct{}
718-
if vals.InMemoryDatabase {
752+
if vals.InMemoryDatabase.Value() {
719753
// This is only used for testing.
720754
options.Database = dbmem.New()
721755
options.Pubsub = pubsub.NewInMemory()
@@ -2733,6 +2767,10 @@ func signalNotifyContext(ctx context.Context, inv *serpent.Invocation, sig ...os
27332767
}
27342768

27352769
func getAndMigratePostgresDB(ctx context.Context, logger slog.Logger, postgresURL string, auth codersdk.PostgresAuth, sqlDriver string) (*sql.DB, string, error) {
2770+
// The postgresURL is constructed earlier by caller from either the
2771+
// CODER_PG_CONNECTION_URL or the individual components (host, port, etc.)
2772+
// So we just validate it here and don't need to reconstruct it
2773+
27362774
dbURL, err := escapePostgresURLUserInfo(postgresURL)
27372775
if err != nil {
27382776
return nil, "", xerrors.Errorf("escaping postgres URL: %w", err)

cli/testdata/coder_server_--help.golden

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ OPTIONS:
6161
server postgres-builtin-url". Note that any special characters in the
6262
URL must be URL-encoded.
6363

64+
--postgres-database string, $CODER_PG_DATABASE
65+
PostgreSQL database name. Used as an alternative to postgres-url.
66+
67+
--postgres-host string, $CODER_PG_HOST
68+
PostgreSQL database host. Used as an alternative to postgres-url for
69+
providing individual components of the database connection.
70+
71+
--postgres-options string, $CODER_PG_OPTIONS
72+
PostgreSQL connection options. Used as an alternative to postgres-url.
73+
74+
--postgres-password string, $CODER_PG_PASSWORD
75+
PostgreSQL database password. Used as an alternative to postgres-url.
76+
77+
--postgres-port string, $CODER_PG_PORT (default: 5432)
78+
PostgreSQL database port. Defaults to 5432 if not specified.
79+
80+
--postgres-username string, $CODER_PG_USERNAME
81+
PostgreSQL database username. Used as an alternative to postgres-url.
82+
6483
--ssh-keygen-algorithm string, $CODER_SSH_KEYGEN_ALGORITHM (default: ed25519)
6584
The algorithm to use for generating ssh keys. Accepted values are
6685
"ed25519", "ecdsa", or "rsa4096".

cli/testdata/server-config.yaml.golden

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,25 @@ ephemeralDeployment: false
466466
# authentication (awsiamrds) is recommended.
467467
# (default: password, type: enum[password\|awsiamrds])
468468
pgAuth: password
469+
# PostgreSQL database host. Used as an alternative to postgres-url for providing
470+
# individual components of the database connection.
471+
# (default: <unset>, type: string)
472+
pgHost: ""
473+
# PostgreSQL database port. Defaults to 5432 if not specified.
474+
# (default: 5432, type: string)
475+
pgPort: "5432"
476+
# PostgreSQL database username. Used as an alternative to postgres-url.
477+
# (default: <unset>, type: string)
478+
pgUsername: ""
479+
# PostgreSQL database password. Used as an alternative to postgres-url.
480+
# (default: <unset>, type: string)
481+
pgPassword: ""
482+
# PostgreSQL database name. Used as an alternative to postgres-url.
483+
# (default: <unset>, type: string)
484+
pgDatabase: ""
485+
# PostgreSQL connection options. Used as an alternative to postgres-url.
486+
# (default: <unset>, type: string)
487+
pgOptions: ""
469488
# A URL to an external Terms of Service that must be accepted by users when
470489
# logging in.
471490
# (default: <unset>, type: string)

enterprise/cli/testdata/coder_server_--help.golden

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ OPTIONS:
6262
server postgres-builtin-url". Note that any special characters in the
6363
URL must be URL-encoded.
6464

65+
--postgres-database string, $CODER_PG_DATABASE
66+
PostgreSQL database name. Used as an alternative to postgres-url.
67+
68+
--postgres-host string, $CODER_PG_HOST
69+
PostgreSQL database host. Used as an alternative to postgres-url for
70+
providing individual components of the database connection.
71+
72+
--postgres-options string, $CODER_PG_OPTIONS
73+
PostgreSQL connection options. Used as an alternative to postgres-url.
74+
75+
--postgres-password string, $CODER_PG_PASSWORD
76+
PostgreSQL database password. Used as an alternative to postgres-url.
77+
78+
--postgres-port string, $CODER_PG_PORT (default: 5432)
79+
PostgreSQL database port. Defaults to 5432 if not specified.
80+
81+
--postgres-username string, $CODER_PG_USERNAME
82+
PostgreSQL database username. Used as an alternative to postgres-url.
83+
6584
--ssh-keygen-algorithm string, $CODER_SSH_KEYGEN_ALGORITHM (default: ed25519)
6685
The algorithm to use for generating ssh keys. Accepted values are
6786
"ed25519", "ecdsa", or "rsa4096".

0 commit comments

Comments
 (0)