Files
coder/cli/config/builtin_postgres.go
T
Steven Masley 462b474729 fix: refuse unknown built-in PostgreSQL data versions (#32489)
<!-- Authored with Coder Agents on behalf of @Emyrk -->

Refs
https://linear.app/codercom/issue/CORE-634/upgrade-embedded-postgresql-to-a-non-eol-version.
Precedes #32144.

Refuse to start built-in PostgreSQL when an existing data directory has
a missing or unsupported version, rather than letting embedded-postgres
replace its data.

- An existing `PG_VERSION` other than 13, including 18 or 19, now
returns an error without starting PostgreSQL.
- A missing marker also returns recovery guidance instead of
initializing over existing data.
- Fresh databases and existing PostgreSQL 13 databases keep their
current behavior.
- Development database recovery uses the same guard.

<details><summary>Agent context</summary>

The bundled embedded-postgres v1 library can delete an existing data
directory whose version does not match its PostgreSQL 13 binary. This
change checks the directory before invoking the library, without
changing the binary version or migrating data. Only Coder builds
containing this guard gain the protection; already-released older
binaries remain unchanged.

</details>

---

<sub>Coder Agents on behalf of @Emyrk.</sub>
2026-11-20 13:43:07 -06:00

40 lines
1.3 KiB
Go

package config
import (
"errors"
"os"
"path/filepath"
"strings"
"golang.org/x/xerrors"
)
// ValidateBuiltinPostgresVersion refuses to start over existing data that this
// Coder release cannot read. Embedded-postgres otherwise removes that data.
func ValidateBuiltinPostgresVersion(postgresPath string) error {
dataPath := filepath.Join(postgresPath, "data")
_, err := os.Stat(dataPath)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return xerrors.Errorf("inspect built-in PostgreSQL data directory: %w", err)
}
versionPath := filepath.Join(dataPath, "PG_VERSION")
contents, err := os.ReadFile(versionPath)
if err != nil {
advice := "Check that Coder can read the file and data directory before retrying."
if errors.Is(err, os.ErrNotExist) {
advice = "Restore the data directory from a backup, or if this is an unused new deployment, move it aside and restart. Do not create PG_VERSION manually."
}
return xerrors.Errorf("read built-in PostgreSQL version in %s: %w; data was not changed. %s", dataPath, err, advice)
}
version := strings.TrimSuffix(string(contents), "\n")
if version != "13" {
return xerrors.Errorf("unsupported built-in PostgreSQL version %q in %s; data was not changed", version, versionPath)
}
return nil
}