Database migration error when launching n8n with existing database tables

I’m trying to migrate my n8n version 1.51.2 setup to Google Cloud Platform using Cloud SQL and Cloud Run. The process involved exporting data from my old PostgreSQL database and importing it into the new Cloud SQL instance.

Everything seemed to work fine during the data transfer. The Docker container deploys correctly and establishes a connection to the Cloud SQL database. However, when n8n starts up, I encounter this error:

Error: There was an error running database migrations
QueryFailedError: relation "migrations" already exists

This is confusing because the migrations table is already present in the database from the imported data. Why would n8n try to create this table again? I’m not sure what triggers the migration process or how n8n determines when to run migrations.

I expected n8n to start normally without attempting any database migrations since all the tables are already in place.

Had this exact issue when moving my n8n instance between environments. The problem occurs because n8n checks the migrations table structure against its expected schema version, not just whether the table exists. When you import data from an older version, the migrations table might be missing recent migration records that your current n8n version expects to see. What worked for me was connecting directly to the Cloud SQL database and checking the migrations table contents. Compare the migration entries with what your n8n version expects. You’ll likely find some missing migration records that correspond to versions between your old setup and current version. The safest approach is to manually insert the missing migration records with their proper timestamps, or alternatively, truncate the migrations table and let n8n rebuild it from scratch. Just make sure to backup your data first since this affects how n8n interprets your database state.

sounds like your migration table got corrupted during the import process. try dropping just the migrations table and restart n8n - it should recreate it automatically while keeping your data intact. worked for me when i had similar issues with postgres imports.

This migration error typically happens when there’s a version mismatch between your imported database schema and the n8n binary you’re running. When n8n starts, it doesn’t just check if tables exist - it validates the entire migration history to ensure database consistency with the application version. Since you’re upgrading from 1.51.2, there are likely intermediate migrations that your imported database never executed. The Cloud Run environment is trying to apply these missing migrations but encounters conflicts with existing table structures. I’ve seen this resolved by either downgrading temporarily to match your original database version and then performing a proper sequential upgrade, or by clearing the migrations table entirely and letting n8n reinitialize from your existing schema. The latter approach requires careful verification that your current table structure matches what the new version expects. Consider checking n8n’s GitHub issues for version-specific migration problems between your old and new versions.