My alembic migration won’t run! (Finding and killing the process that has a postgres relation locked)

You go to run your migrations against your DB, and everything just hangs. One of the causes for this is that the migration is waiting for access to a relation (typically a table, but potentially a view) to which your migration script needs to write, but another process has it locked. If that’s the reason, here’s how we find and kill that process.

That’ll give you the ID of the relation you need.

That’ll give you the pid of the locking process.

In postgres itself, you can do a

but that doesn’t work if you aren’t a superuser. Just pop back to your command line and kill the process itself.