{"record":{"id":"df631b3894e64876","repo":"plandex-ai/plandex","slug":"error-creating-postgres-driver-v","errorCode":null,"errorMessage":"error creating postgres driver: %v","messagePattern":"error creating postgres driver: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"app/server/db/db.go","lineNumber":117,"sourceCode":"\t\tmigrationsDir = os.Getenv(\"MIGRATIONS_DIR\")\n\t}\n\n\treturn migrationsUp(migrationsDir)\n}\n\nfunc MigrationsUpWithDir(dir string) error {\n\treturn migrationsUp(dir)\n}\n\nfunc migrationsUp(dir string) error {\n\tif Conn == nil {\n\t\treturn errors.New(\"db not initialized\")\n\t}\n\n\tdriver, err := postgres.WithInstance(Conn.DB, &postgres.Config{})\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error creating postgres driver: %v\", err)\n\t}\n\n\tm, err := migrate.NewWithDatabaseInstance(\n\t\t\"file://\"+dir,\n\t\t\"postgres\", driver)\n\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error creating migration instance: %v\", err)\n\t}\n\n\t// Uncomment below (and update migration version) to reset migration state to a specific version after a failure\n\t// if os.Getenv(\"GOENV\") == \"development\" {\n\t// \tmigrateVersion := 2025052900\n\t// \tif err := m.Force(migrateVersion); err != nil {\n\t// \t\treturn fmt.Errorf(\"error forcing migration version: %v\", err)\n\t// \t}\n\t// }\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/server/db/db.go#L99-L135","documentation":"migrationsUp uses golang-migrate's postgres.WithInstance to wrap the existing sqlx DB handle as a migration driver. This error wraps a failure in that wrapping step, typically because the underlying *sql.DB is unhealthy or already closed.","triggerScenarios":"MigrationsUp/MigrationsUpWithDir called when Conn is non-nil but the pooled connection is closed/broken (postgres restart, idle timeout), or the postgres driver cannot configure itself against the handle.","commonSituations":"Running migrations in a job where the DB was closed earlier; database restarted between Connect and migrate; health-check race where Conn exists but ping fails.","solutions":["Check the wrapped %v error and ping the DB before running migrations (Conn.Ping())","If the pool is stale, re-run Connect/MustInitDb to rebuild Conn, then retry migrations","Ensure migrations run before any code closes the DB handle","Verify postgres is reachable and accepting connections"],"exampleFix":"// before\ndriver, err := postgres.WithInstance(Conn.DB, &postgres.Config{})\n// after\nif err := Conn.Ping(); err != nil {\n    MustInitDb() // rebuild stale connection pool\n}\ndriver, err := postgres.WithInstance(Conn.DB, &postgres.Config{})","handlingStrategy":"validation","validationCode":"if Conn == nil || Conn.DB == nil {\n    return errors.New(\"db not initialized\")\n}\nif err := Conn.Ping(); err != nil {\n    return fmt.Errorf(\"db connection unhealthy before migrations: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := MigrationsUp(); err != nil {\n    if strings.Contains(err.Error(), \"error creating postgres driver\") {\n        MustInitDb() // rebuild stale pool, then retry once\n        return MigrationsUp()\n    }\n    return err\n}","preventionTips":["Ping the DB before running migrations","Run migrations once at startup, before any code can close the pool","Health-check postgres reachability in deployment scripts","Avoid sharing the migration driver across goroutines"],"tags":["database","migrations","golang-migrate","postgresql"],"backgroundTag":"migration-driver-creation-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}