flipped-aurora/gin-vue-admin · error
postgresql config invalid
Error message
postgresql config invalid
What it means
PgsqlInitHandler.WriteConfig persists PostgreSQL init settings to config.yaml (DbType="pgsql", new JWT signing key). It type-asserts the context value "config" to config.Pgsql; on failure it returns "postgresql config invalid". The error signals the context was not prepared by the pgsql EnsureDB.
Source
Thrown at server/service/system/sys_initdb_pgsql.go:31
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type PgsqlInitHandler struct{}
func NewPgsqlInitHandler() *PgsqlInitHandler {
return &PgsqlInitHandler{}
}
// WriteConfig pgsql 回写配置
func (h PgsqlInitHandler) WriteConfig(ctx context.Context) error {
c, ok := ctx.Value("config").(config.Pgsql)
if !ok {
return errors.New("postgresql config invalid")
}
global.GVA_CONFIG.System.DbType = "pgsql"
global.GVA_CONFIG.Pgsql = c
global.GVA_CONFIG.JWT.SigningKey = uuid.New().String()
cs := utils.StructToMap(global.GVA_CONFIG)
for k, v := range cs {
global.GVA_VP.Set(k, v)
}
global.GVA_ACTIVE_DBNAME = &c.Dbname
return global.GVA_VP.WriteConfig()
}
// EnsureDB 创建数据库并初始化 pg
func (h PgsqlInitHandler) EnsureDB(ctx context.Context, conf *request.InitDB) (next context.Context, err error) {
if s, ok := ctx.Value("dbtype").(string); !ok || s != "pgsql" {
return ctx, ErrDBTypeMismatch
}
View on GitHub (pinned to 3136500ef3)
Solutions
- Call WriteConfig with the context returned by PgsqlInitHandler.EnsureDB, which stores conf.ToPgsqlConfig() under key "config".
- Use InitDBService.InitDB with DBType="pgsql" so pairing is handled for you.
- Audit custom pipelines to ensure each DB handler's EnsureDB output flows to its own WriteConfig.
- If needed manually: ctx = context.WithValue(ctx, "config", conf.ToPgsqlConfig()).
Example fix
// before ctx, _ = mssqlHandler.EnsureDB(ctx, &conf) err = pgsqlHandler.WriteConfig(ctx) // -> postgresql config invalid // after ctx, _ = pgsqlHandler.EnsureDB(ctx, &conf) err = pgsqlHandler.WriteConfig(ctx)
Defensive patterns
Strategy: type-guard
Validate before calling
if _, ok := ctx.Value("config").(config.Pgsql); !ok {
return errors.New("postgresql config missing in context")
} Type guard
func hasPgsqlConfig(ctx context.Context) bool {
_, ok := ctx.Value("config").(config.Pgsql)
return ok
} Try / catch
err := handler.WriteConfig(ctx)
if err != nil && err.Error() == "postgresql config invalid" {
// ctx not produced by PgsqlInitHandler.EnsureDB; rerun EnsureDB first
} Prevention
- Only feed WriteConfig the context from PgsqlInitHandler.EnsureDB.
- Keep one DB handler per init run; never mix pipelines.
- Prefer InitDBService.InitDB with DBType="pgsql".
- Test the EnsureDB -> WriteConfig sequence for pgsql.
When it happens
Trigger: Calling PgsqlInitHandler.WriteConfig(ctx) when ctx's "config" value is missing or holds config.Mysql/Mssql/Sqlite instead of config.Pgsql.
Common situations: Cross-handler misuse (e.g. mysql EnsureDB feeding pgsql WriteConfig); hand-rolled init pipelines; tests calling WriteConfig in isolation.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/8d5c0930e46916b0.
Report an issue: GitHub.