Sequelize "sequelize db:migrate" Fails in CI
sequelize-cli ran your migrations and one of two things went wrong: it could not connect to the database, or a migration’s up threw. The error text distinguishes a connectivity problem from a migration-content problem.
What this error means
npx sequelize-cli db:migrate fails either with a connection error (refused/access denied) before anything runs, or with a thrown error from inside a specific migration. The two need different fixes.
ERROR: connect ECONNREFUSED 127.0.0.1:5432
# or
== 20240115-add-status: migrating =======
ERROR: column "status" of relation "orders" already existsCommon causes
Cannot connect - wrong config/env for this NODE_ENV
sequelize-cli reads config/config.js keyed by NODE_ENV. If the CI environment’s entry has the wrong host/port/credentials (or the database is not ready), the connection fails first.
A migration’s up() throws
A duplicate column, missing table, or constraint violation inside a migration makes db:migrate fail at that step. It is deterministic and points at the specific migration.
SequelizeMeta disagrees with the schema
If SequelizeMeta thinks a migration has not run but its objects exist (or vice versa), the next run re-applies or skips incorrectly and errors.
How to fix it
For connection errors, fix config and readiness
Make sure the config entry for the CI NODE_ENV matches the reachable database, and wait for it to be ready.
export NODE_ENV=test
until pg_isready -h "$DB_HOST" -p "$DB_PORT"; do sleep 1; done
npx sequelize-cli db:migrateFor a throwing migration, fix the SQL and run clean
- Read which migration threw and why (already-exists vs missing object).
- Run migrations against a fresh CI database so each
upexecutes once. - Correct the migration if it genuinely conflicts with a prior one.
Reconcile SequelizeMeta
On a non-clean database, align the meta table with reality before continuing.
npx sequelize-cli db:migrate:status # see applied vs pendingHow to prevent it
- Set
NODE_ENVand keep the matchingconfigentry aligned with the CI database. - Run migrations against an ephemeral database so each
upruns once. - Use
db:migrate:statusin CI to catch meta/schema drift.