Skip to main content

Postgres DB diff

Command line tool to compare two PostgreSQL databases. It is based on parsing psql meta commands output. Such as \dt for tables and \dv for views.

https://www.postgresql.org/docs/current/static/app-psql.html

How to install

There are two options:

  1. Use any python package installing tool. Recommended pip.

  2. Just copy/paste postgresdbdiff.py into your dir and run it using python postgresdbdiff.py

Usage

usage: postgresdbdiff.py [-h] --db1 DB1 --db2 DB2 [--diff-folder DIFF_FOLDER]

optional arguments:
  -h, --help            show this help message and exit
  --db1 DB1             First DB name
  --db2 DB2             Second DB name
  --diff-folder DIFF_FOLDER
                        Directory to output diffs

Example

Create two DBs. One using this SQL:

CREATE TABLE table_a (
  id INTEGER PRIMARY KEY,
  test_unique VARCHAR (100) UNIQUE,
  test_not_null VARCHAR (100) NOT NULL,
  test_checks INTEGER NOT NULL
);

CREATE TABLE table_b (
  id INTEGER PRIMARY KEY,
  table_a_id integer REFERENCES table_a (id)
);

CREATE TABLE table_c (
  id INTEGER PRIMARY KEY
);

CREATE VIEW view_a AS SELECT
  id, test_unique, 42 AS some_number
FROM table_a;

Other using this SQL:

CREATE TABLE table_a (
  id INTEGER PRIMARY KEY,
  test_unique VARCHAR (100),
  test_not_null VARCHAR (100),
  test_checks INTEGER NOT NULL CHECK (test_checks > 0)
);

CREATE TABLE table_b (
  id INTEGER PRIMARY KEY,
  table_a_no integer REFERENCES table_a (id)
);

CREATE VIEW view_a AS SELECT
  id, test_unique
FROM table_a;

Then run this command

python postgresdbdiff.py --db1 diff_a --db2 diff_b --diff-folder diffs

Output should be like this

TABLES: additional in "diff_a"
  table_c

TABLES: not matching
  table_a
  table_b

VIEWS: not matching
  view_a

And there should be the folder named diffs with files looking like this

# diffs/table_a.diff
--- TABLES.diff_a.table_a
+++ TABLES.diff_b.table_a
@@ -1,12 +1,13 @@
                          Table "public.table_a"
     Column     |          Type          | Collation | Nullable | Default
 ---------------+------------------------+-----------+----------+---------
  id            | integer                |           | not null |
  test_checks   | integer                |           | not null |
- test_not_null | character varying(100) |           | not null |
+ test_not_null | character varying(100) |           |          |
  test_unique   | character varying(100) |           |          |
 Indexes:
     "table_a_pkey" PRIMARY KEY, btree (id)
-    "table_a_test_unique_key" UNIQUE CONSTRAINT, btree (test_unique)
+Check constraints:
+    "table_a_test_checks_check" CHECK (test_checks > 0)
 Referenced by:
-    TABLE "table_b" CONSTRAINT "table_b_table_a_id_fkey" FOREIGN KEY (table_a_id) REFERENCES table_a(id)
+    TABLE "table_b" CONSTRAINT "table_b_table_a_no_fkey" FOREIGN KEY (table_a_no) REFERENCES table_a(id)


# diffs/table_b.diff
--- TABLES.diff_a.table_b
+++ TABLES.diff_b.table_b
@@ -1,9 +1,9 @@
                 Table "public.table_b"
    Column   |  Type   | Collation | Nullable | Default
 ------------+---------+-----------+----------+---------
  id         | integer |           | not null |
- table_a_id | integer |           |          |
+ table_a_no | integer |           |          |
 Indexes:
     "table_b_pkey" PRIMARY KEY, btree (id)
 Foreign-key constraints:
-    "table_b_table_a_id_fkey" FOREIGN KEY (table_a_id) REFERENCES table_a(id)
+    "table_b_table_a_no_fkey" FOREIGN KEY (table_a_no) REFERENCES table_a(id)


# diffs/view_a.diff
--- VIEWS.diff_a.view_a
+++ VIEWS.diff_b.view_a
@@ -1,6 +1,5 @@
                          View "public.view_a"
    Column    |          Type          | Collation | Nullable | Default
 -------------+------------------------+-----------+----------+---------
  id          | integer                |           |          |
- some_number | integer                |           |          |
  test_unique | character varying(100) |           |          |

Metadata

Release files for postgres-db-diff 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postgres-db-diff 0.9.1
File Size Uploaded Provenance
postgres-db-diff-0.9.1.tar.gz 5.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for postgres-db-diff 0.9.1
File Interpreter ABI Platform
postgres_db_diff-0.9.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.1 kB

Release files / postgres-db-diff-0.9.1.tar.gz

Download URL postgres-db-diff-0.9.1.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5fbec7e4daad9b9de7a25cae3f81ffe04e2135e2cd8848c231fc04ae9e43302c
BLAKE2b-256 checksum
How to use checksums
996fab96b4cc516e893ff84a879f9cc568feaf50885677a20835f8dc91d78b0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / postgres_db_diff-0.9.1-py2.py3-none-any.whl

Download URL postgres_db_diff-0.9.1-py2.py3-none-any.whl
Size 7.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
0ac4f248e2059e2d25add34a754e787ab06fb687b79c2b6c12b8fe44f3ba728f
BLAKE2b-256 checksum
How to use checksums
23ea91e8d7b2bee6fa8be8f2386c43c93c30c615d02d0b07db045f55673608f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for postgres-db-diff 0.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page