使用HangFire 1.6.22与PostgreSQL启动失败,报错原因咨询
Hey there, this error pops up when HangFire's PostgreSQL storage tries to run its auto-install/upgrade scripts but finds that the updatecount column on the lock table already exists. Usually this happens because a previous migration didn't finish cleanly, or someone manually modified the DB schema which threw off HangFire's auto-migration checks.
Here are a few practical ways to fix this:
Option 1: Manually Mark the Migration as Completed
- First, shut down your application process to avoid conflicting DB operations.
- Connect to your PostgreSQL database and run this query to confirm the column is indeed present:
SELECT column_name FROM information_schema.columns WHERE table_name = 'lock' AND column_name = 'updatecount';
- If you get a result back, the column exists. Now we need to tell HangFire to skip this duplicate migration step. Check the migration history table (usually located in
hangfire_schema.schemaversions):
SELECT * FROM hangfire_schema.schemaversions;
Find the version entry that corresponds to adding the updatecount column to the lock table, then set its applied value to true. If the entry doesn't exist, you can insert it manually with the correct version number and applied = true.
Option 2: Disable Auto-Migration and Run Scripts Manually
If auto-migration keeps tripping up on this conflict, take control with manual migration:
- When configuring your HangFire storage, add the
DisableAutomaticMigrationflag to skip auto-run scripts:
var storage = new PostgreSqlStorage("your_connection_string", new PostgreSqlStorageOptions { DisableAutomaticMigration = true });
- Grab the migration scripts for HangFire.PostgreSql 1.6.22 (from the official source code), cross-check which scripts you've already executed, and run only the remaining ones—make sure to skip the script that attempts to create the
updatecountcolumn since it's already there. - Once done, you can re-enable auto-migration by setting
DisableAutomaticMigrationback tofalse, or leave it off if you prefer managing migrations manually going forward.
Option 3: Reset the HangFire DB (Use with Caution)
If you don't need to preserve existing HangFire job data, a full reset might be the fastest fix:
- Back up any HangFire-related tables if you need to save critical data.
- Delete all HangFire tables and the
hangfire_schemaschema entirely. - Restart your application—HangFire will re-run the full initialization script, creating all required tables and columns from scratch without conflicts.
Important Notes
- Always back up your database before making any changes, especially in production environments.
- Test these fixes in a staging environment first to avoid unexpected issues.
- HangFire 1.6.22 is a fairly old version—consider upgrading to a newer release of HangFire.PostgreSql if possible, as newer versions have more robust migration logic and fewer edge cases.
内容的提问来源于stack exchange,提问作者Jophy job

