Rolling back schema changes is inherently risky and may result in data loss. Always test rollback procedures in non-production environments and ensure you have complete database backups before proceeding.
The pgschema approach to rollback is to revert your schema file to a previous version and apply the changes using the standard plan/apply workflow. This ensures rollbacks follow the same safety mechanisms as forward migrations.
1
Revert Schema File
Use git to revert your schema file to the previous working version.
Copy
# Revert to previous commit (most common case)git checkout HEAD~1 schema.sql# Or find and revert to a specific commitgit log --oneline schema.sqlgit checkout <commit-hash> schema.sql
This restores your schema file to the state before the problematic migration was applied.
2
Generate Rollback Plan
Create and review a migration plan that will rollback the database to match the reverted schema file.
Copy
# Generate rollback planpgschema plan --host localhost --db myapp --user postgres --file schema.sql \ --output-json rollback_plan.json \ --output-human rollback_plan.txt# Review the rollback plan carefullycat rollback_plan.txt
The plan will show exactly what changes pgschema will make to rollback the database to the previous schema state.
3
Execute Rollback
Apply the rollback plan with appropriate safety measures for production environments.