go-dbcompare
go-dbcompare is a Go-based tool designed to compare two PostgreSQL databases and identify differences in table structures and data. The results can be exported to an Excel file for further analysis.
Features
- Compare tables between two databases.
- Identify missing or extra records in either database.
- Export comparison results to an Excel file.
Installation
To install and use go-dbcompare, follow these steps:
Clone the Repository
git clone https://github.com/CDavidSV/go-dbcompare.git
cd go-dbcompare
Build the Project
go build -o dbcompare main.go
Usage
The application has two main commands: compare and generate.
Generate Command
The generate command helps you create configuration files or DSN strings for database connections.
Generate Configuration File
Interactive mode that guides you through entering database connection details:
./dbcompare generate config
You can also provide flags to skip the prompts:
./dbcompare generate config \
--db1-name "Production" \
--db1-host "db1.example.com" \
--db1-port 5432 \
--db1-database "postgres" \
--db1-username "admin" \
--db1-password "password" \
--db2-name "Development" \
--db2-host "db2.example.com" \
--db2-port 5432 \
--db2-database "postgres" \
--db2-username "admin" \
--db2-password "password"
Optional connection parameters can be added with --db1-param and --db2-param:
./dbcompare generate config \
--db1-name "Production" \
--db1-host "db1.example.com" \
--db1-port 5432 \
--db1-database "postgres" \
--db1-username "admin" \
--db1-password "password" \
--db1-param "sslmode=disable" \
--db1-param "connect_timeout=10"
This creates a db-compare-config.yml file with the following structure:
database1:
name: Production Database
host: db1.example.com
port: 5432
database: postgres
username: admin
password: "1234"
database2:
name: Development Database
host: db2.example.com
port: 5432
database: postgres
username: admin
password: "1234"
Generate DSN String
Generates a Data Source Name (DSN) connection string for direct use:
./dbcompare generate dsn \
--host db1.example.com \
--user admin \
--password mypassword \
--database postgres \
--port 5432 \
--param "sslmode=disable"
Flags:
--host, -s (required): Database hostname or IP address
--user, -u (required): Database username
--password, -a (required): Database password
--database, -d (required): Database name
--port, -p: Port number (default: 5432)
--param, -e: Optional connection parameters (can be repeated)
Compare Command
Runs the comparison between two databases and exports results to an Excel file.
Using a Configuration File
./dbcompare compare -o "./results"
Or with a custom config file:
./dbcompare compare --config "./my-config.yml" --output "./results"
Using DSN Strings (Direct Connection)
You can also connect directly without a config file using DSN strings:
./dbcompare compare \
--dsn1 "postgres://admin:password@db1.example.com:5432/postgres" \
--dsn2 "postgres://admin:password@db2.example.com:5432/postgres" \
--output "./results"
Compare Command Flags:
--config, -c: Path to the configuration file (default: ./db-compare-config.yml)
--output, -o: Output directory for the comparison results (default: ./)
--name, -n: Custom name for the output file (without extension). If not provided, a timestamp is used
--type, -t: Format of the output file (default: xlsx, options: xlsx or csv)
--dsn1: DSN connection string for the first database (bypasses config file)
--dsn2: DSN connection string for the second database (bypasses config file)
Examples
Compare using a config file and save with a custom name:
./dbcompare compare --config "./config.yml" --output "./results" --name "production_vs_staging"
Compare using DSN strings directly:
./dbcompare compare \
--dsn1 "postgres://user:pass@host1:5432/db" \
--dsn2 "postgres://user:pass@host2:5432/db" \
--output "./results" \
--name "comparison_result"
The comparison output is saved as an Excel file (.xlsx) containing the differences between the two databases.
Future Improvements
- Schema comparison for detecting index and constraint differences.
- Support for multiple database systems.
- Additional output formats (HTML, PDF reports).
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
License
This project is licensed under the MIT License. See the LICENSE file for details.