How to compare data between two databases in PostgreSQL?

There are several tools out there:

(note that most of these tools can only compare structure, but not data)

Free Ones:

  • pgquarrel: http://eulerto.github.io/pgquarrel (schema diff)
  • apgdiff http://apgdiff.com/ (schema diff)
  • Liquibase (Cross DBMS): http://www.liquibase.org (schema diff)
  • pgAdmin https://www.pgadmin.org (schema diff in pgAdmin4)
  • WbDataDiff (Cross DBMS): http://www.sql-workbench.net/manual/compare-commands.html#command-data-diff (data diff)
  • WbSchemaDiff (Cross DBMS): http://www.sql-workbench.net/manual/compare-commands.html
  • Migra https://migra.djrobstep.com/ (schema diff)

Commercial:

  • DB Comparer: http://www.sqlmanager.net/en/products/postgresql/dbcomparer
  • Aqua Data Studio: http://docs.aquafold.com/docs-diff-schema.html
  • DB Solo: http://www.dbsolo.com/index.html (30 day trial)
  • PostgresCompare: https://www.postgrescompare.com/ (14 day trial, compares schema and data)

Try using pg_dump on both databases and diffing the files.


Another free app (that can only compare structure, but not data):

DBeaver - you can select databases, tables, etc to compare with each other


I have evaluated a lot of the tools and found following solution:

Schema comparison:

The most interesting were Liquibase, Persyas and PgCodeKeeper:

(issue) Liquebase converts:

 SET DEFAULT nextval('myschema.name_id_seq'::regclass)

into

BIGSERIAL

So it was rejected to use

(issue) Persyas worked fine untill I added some additional schema and it starts throwing following:

pyrseas_1       | TypeError: 'NoneType' object is not iterable

So I have found PgCodeKeeper it works perfectly and it is alive (you can check releases). I use following command:

./pgcodekeeper-cli.sh -E -I ./ignore.txt \
-s "jdbc:postgresql://localhost/postgres?user=postgres&password=123" \
-t "jdbc:postgresql://localhost/postgres?user=postgres&password=123" \
-o /result/schema-diff-$(date +'%m%d%y_%H%M%S').sql

Data comparison: I have tried to use Liquebase and it just does not work you can see the steps I tried in my unanswered question about data difference of two databases with Liquebase

So I have found another project SQL Workbench/J It works really nice and generate reall diff in sql. I use following command:

  java -jar /sqlworkbench.jar -command="'WbDataDiff -excludeTables=$EXCLUDE_TABLES \
-referenceConnection=$REFERENCE_CONNECTION \ 
-targetConnection=$TARGET_CONNECTION -referenceSchema=$1 \
-targetSchema=$1  -file=$DIFF_RESULT_FILE -singleFile=true \
-includeDelete=true -ignoreMissingTarget=true ;'"

Both tools support objects filtration. It is really convenient.

Migrations

And finally I use Liquebase just for migration/version tracking.