
Database Diff Tool
FreeAutomate database schema comparison and synchronization.
Free · Opens the source repo
What Database Diff Tool does
The Database Diff Tool is designed for developers and database administrators who need to compare and synchronize database schemas across different environments, such as development and production. This tool automates the process of identifying differences between two database schemas, making it easier to manage changes and ensure consistency. By extracting schemas from both source and target databases, users can quickly identify added, removed, or modified elements, such as tables, columns, indexes, and constraints.
To use the tool, users must have connection credentials for both databases and the necessary permissions to access schema information. The tool relies on standard database commands like pg_dump for PostgreSQL and mysqldump for MySQL to extract schema details. It then performs a comprehensive comparison, generating a structured report that categorizes differences and provides migration SQL scripts to align the target database with the source. This ensures that any changes made in one environment can be accurately reflected in another, reducing the risk of discrepancies.
The Database Diff Tool is particularly useful in scenarios where multiple environments are in use, such as during development cycles or when preparing for production releases. It helps teams maintain schema integrity by providing clear visibility into changes and allowing for easy synchronization. Furthermore, this tool supports rollback operations, ensuring that migrations can be reversed if necessary, which adds an extra layer of safety during deployment processes.
Overall, the Database Diff Tool streamlines the schema comparison process, making it a valuable asset for any team managing multiple database environments. Its automation capabilities save time and reduce the potential for human error, allowing developers to focus on building features rather than managing database inconsistencies.
When to use it
Use this tool when you need to compare and synchronize database schemas between different environments, such as development and production.
When not to use it
This tool is not suitable for real-time database monitoring or for environments where schema changes are frequent and unpredictable without proper migration management.
What you can build with it
Detecting schema drift between environments
After a period of development, use the tool to identify unauthorized changes made directly in production that are not reflected in staging.
Pre-deployment validation
Run the tool before deploying migrations to catch any discrepancies that could lead to deployment failures.
Version upgrades comparison
When upgrading PostgreSQL versions, compare schemas to ensure compatibility and identify any deprecated features.
How to install Database Diff Tool
View source1. Install with the skills CLI
npx skills add jeremylongshore/claude-code-plugins-plus-skills/comparing-database-schemas --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by jeremylongshoreDatabase Diff Tool
Overview
Compare database schemas between two environments (development vs. staging, staging vs.
Prerequisites
- Connection credentials to both source and target databases
psqlormysqlCLI configured to connect to both environments- Read access to
information_schemaandpg_catalog(PostgreSQL) orinformation_schema(MySQL) - Permission to run
pg_dump --schema-onlyfor full schema extraction - Understanding of which environment is the "source of truth" (typically the migration-managed environment)
Instructions
-
Extract the full schema from both databases for comparison:
- PostgreSQL:
pg_dump --schema-only --no-owner --no-privileges -f schema_source.sql source_dband repeat for target_db - MySQL:
mysqldump --no-data --routines --triggers source_db > schema_source.sql - Alternatively, query
information_schemadirectly for programmatic comparison
- PostgreSQL:
-
Compare tables present in each database:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_catalog = 'source_db' EXCEPT SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_catalog = 'target_db'- This reveals tables that exist in source but not in target (and vice versa)
-
Compare columns for each shared table:
- Query
information_schema.columnsfrom both databases for:column_name,data_type,character_maximum_length,is_nullable,column_default,ordinal_position - Flag differences in data type, nullability, default values, and column ordering
- Detect added columns (in source, not target) and dropped columns (in target, not source)
- Query
-
Compare indexes:
- PostgreSQL: Query
pg_indexesforindexname,indexdefon each database - MySQL: Query
information_schema.STATISTICSforINDEX_NAME,COLUMN_NAME,NON_UNIQUE - Flag missing, extra, or differently-defined indexes
- PostgreSQL: Query
-
Compare constraints (primary keys, foreign keys, unique, check):
- Query
information_schema.table_constraintsandinformation_schema.key_column_usage - Detect missing foreign keys, changed constraint names, and altered check constraint expressions
- Query
-
Compare functions, stored procedures, and triggers:
- PostgreSQL: Query
pg_procfor function signatures andpg_triggerfor trigger definitions - MySQL: Query
information_schema.ROUTINESandinformation_schema.TRIGGERS - Compare function bodies for logical differences
- PostgreSQL: Query
-
Compare enum types and custom types (PostgreSQL):
- Query
pg_typeandpg_enumfor enum label differences - Detect added or removed enum values (note: PostgreSQL only supports adding enum values, not removing)
- Query
-
Generate a structured diff report categorizing differences as:
- Added: Objects in source not present in target (require CREATE statements)
- Removed: Objects in target not present in source (require DROP statements, confirm intentional)
- Modified: Objects differing between source and target (require ALTER statements)
-
Generate migration SQL to synchronize the target database to match the source:
- CREATE TABLE for new tables, ALTER TABLE ADD COLUMN for new columns
- ALTER TABLE ALTER COLUMN for type changes, ALTER TABLE DROP COLUMN for removed columns
- CREATE INDEX / DROP INDEX for index differences
- Include transaction wrapping and rollback-safe operations
-
Validate the generated migration by applying it to a copy of the target database and re-running the diff. The second diff should report zero differences, confirming the migration produces the expected state.
Output
- Schema diff report listing all differences categorized by type (added, removed, modified)
- Migration SQL script to synchronize target schema to match source
- Rollback SQL script to reverse the migration if needed
- Side-by-side comparison of differing object definitions
- Drift detection summary highlighting changes not tracked in migration files
Error Handling
| Error | Cause | Solution |
|---|---|---|
| Connection refused to one database | Network or credential issue on source or target | Verify connection strings; check firewall rules; confirm credentials work with direct psql or mysql connection |
Permission denied on pg_catalog queries | User lacks read access to system catalogs | Grant pg_read_all_settings role; or use pg_dump --schema-only which requires fewer privileges |
| False positive differences from default value formatting | PostgreSQL normalizes default expressions differently in different versions | Normalize default value strings before comparison; ignore whitespace differences; compare semantic equivalence |
| Enum type modification blocked | PostgreSQL does not support removing enum values or reordering | Create a new enum type, migrate the column, drop the old type; document this as a multi-step migration |
| Generated migration fails on target | Target has data that violates new constraints | Add data validation queries before constraint creation; backfill default values; handle edge cases in migration |
Examples
Detecting schema drift between staging and production: After 3 months without auditing, the diff reveals: 2 columns added to production manually (not in migrations), 1 index missing from staging, and 3 functions with different implementations. A migration script is generated to bring staging in sync, and the manual production changes are backported into migration files.
Pre-deployment schema validation: Before deploying a release with 5 migration files, run the diff between the post-migration staging schema and the expected schema. The diff catches a migration that accidentally dropped a constraint that a later migration depends on. The migration ordering is fixed before production deployment.
Comparing PostgreSQL schemas across major version upgrade: Schema extracted from PostgreSQL 14 and compared against PostgreSQL 16 after migration. Diff reveals function signature changes for built-in function calls, updated default values for new parameters, and deprecated syntax in stored procedures. Migration script updates function definitions for the new version.
Resources
- PostgreSQL information_schema: https://www.postgresql.org/docs/current/information-schema.html
- MySQL information_schema: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html
- pg_dump schema-only mode: https://www.postgresql.org/docs/current/app-pgdump.html
- migra (PostgreSQL schema diff tool): https://github.com/djrobstep/migra
- Skeema (MySQL schema management): https://www.skeema.io/
Frequently asked questions about Database Diff Tool
Similar skills
ClickHouse Logs Queries
Efficiently manage Supabase logs with ClickHouse SQL.
EF Core D2 Database Diagram Generator
Visualize your EF Core models as D2 diagrams effortlessly.
Safe SQL Execution
Ensure secure SQL execution in Supabase applications.
Oracle to PostgreSQL Migration
Identify migration risks between Oracle and PostgreSQL.
SSMA Console
Streamline Oracle to SQL Server migrations with ease.
SQL Performance Optimization
Enhance SQL query efficiency across all databases.
