Categorygithub.com/facefunk/pgdiff
modulepackage
0.9.3-beta.2
Repository: https://github.com/facefunk/pgdiff.git
Documentation: pkg.go.dev

# README

pgdiff - PostgreSQL schema diff

pgdiff compares the schema between two PostgreSQL 9 databases and generates alter statements to be manually run against the second database to make them match. The provided pgdiff.sh script helps automate the process.

pgdiff is transparent in what it does, so it never modifies a database directly. You alone are responsible for verifying the generated SQL before running it against your database. Go ahead and see what SQL gets generated.

pgdiff is written to be easy to expand and improve the accuracy of the diff.

download

osx   linux   windows

usage

pgdiff [options] <schemaType>

(where options and <schemaType> are listed below)

I have found that there is an ideal order for running the different schema types. This order should minimize the problems you encounter. For example, you will always want to add new tables before you add new columns. This is the order that has worked for me, however "your mileage may vary".

In addition, some types can have dependencies which are not in the right order. A classic case is views which depend on other views. The missing view SQL is generated in alphabetical order so if a view create fails due to a missing view, just run the views SQL file over again. The pgdiff.sh script will prompt you about running it again.

Schema type ordering:

  1. ROLE
  2. FUNCTION
  3. SEQUENCE
  4. SCHEMA
  5. SEQUENCE
  6. TABLE
  7. COLUMN
  8. INDEX
  9. VIEW
  10. OWNER
  11. FOREIGN_KEY
  12. GRANT_RELATIONSHIP
  13. GRANT_ATTRIBUTE
  14. TRIGGER

example

I have found it helpful to take --schema-only dumps of the databases in question, load them into a local postgres, then do my sql generation and testing there before running the SQL against a more official database. Your local postgres instance will need the correct users/roles populated because db dumps do not copy that information.

pgdiff -U dbuser -H localhost -D refDB  -O "sslmode=disable" \
       -u dbuser -h localhost -d compDB -o "sslmode=disable" \
       TABLE 

options

optionsexplanation
-V, --versionprints the version of pgdiff being used
-?, --helpdisplays helpful usage information
-U, --user1first postgres user
-u, --user2second postgres user
-W, --password1first db password
-w, --password2second db password
-H, --host1first db host. default is localhost
-h, --host2second db host. default is localhost
-P, --port1first db port number. default is 5432
-p, --port2second db port number. default is 5432
-D, --dbname1first db name
-d, --dbname2second db name
-S, --schema1first schema name. default is public
-s, --schema2second schema name. default is public
-O, --option1first db options. example: sslmode=disable
-o, --option2second db options. example: sslmode=disable

getting started on linux and osx

linux and osx binaries are packaged with an extra, optional bash script and pgrun program that helps speed the diffing process.

  1. download the tgz file for your OS
  2. untar it: tar -xzvf pgdiff.tgz
  3. cd to the new pgdiff directory
  4. edit the db connection defaults in pgdiff.sh
  5. ...or manually run pgdiff for each schema type listed in the usage section above
  6. review the SQL output for each schema type and, if you want to make them match, run it against db2 (Function SQL requires the use of pgrun instead of psql)

getting started on windows

  1. download pgdiff.exe from the bin-win directory on github
  2. edit the db connection defaults in pgdiff.sh or...
  3. manually run pgdiff for each schema type listed in the usage section above
  4. review the SQL output and, if you want to make them match, run it against db2

version history

  1. 0.9.0 - Implemented ROLE, SEQUENCE, TABLE, COLUMN, INDEX, FOREIGN_KEY, OWNER, GRANT_RELATIONSHIP, GRANT_ATTRIBUTE
  2. 0.9.1 - Added VIEW, FUNCTION, and TRIGGER (Thank you, Shawn Carroll AKA SparkeyG)
  3. 0.9.2 - Fixed bug when using the non-default port
  4. 0.9.3 - Added support for schemas other than public. Fixed VARCHAR bug when no max length specified

todo

  1. fix SQL for adding an array column
  2. create windows version of pgdiff.sh (or even better: re-write it all in Go)
  3. allow editing of individual SQL lines after failure (this would probably be done in the script pgdiff.sh)
  4. store failed SQL statements in an error file for later fixing and rerunning?

# Structs

ColumnSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
ForeignKeySchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
FunctionSchema holds a channel streaming function information from one of the databases as well as a reference to the current row of data we're viewing.
GrantAttributeSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
GrantRelationshipSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
IndexSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
OwnerSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
RoleSchema holds a slice of rows from one of the databases as well as a reference to the current row of data we're viewing.
SchemataSchema holds a channel streaming schema meta information from one of the databases as well as a reference to the current row of data we're viewing.
SequenceSchema holds a channel streaming table information from one of the databases as well as a reference to the current row of data we're viewing.
TableSchema holds a channel streaming table information from one of the databases as well as a reference to the current row of data we're viewing.
TriggerSchema holds a channel streaming trigger information from one of the databases as well as a reference to the current row of data we're viewing.
ViewSchema holds a channel streaming view information from one of the databases as well as a reference to the current row of data we're viewing.

# Interfaces

Schema is a database definition (table, column, constraint, indes, role, etc) that can be added, dropped, or changed to match another database.

# Type aliases

ColumnRows is a sortable slice of string maps.
ForeignKeyRows is a sortable string map.
FunctionRows is a sortable slice of string maps.
GrantAttributeRows is a sortable slice of string maps.
GrantRelationshipRows is a sortable slice of string maps.
IndexRows is a sortable slice of string maps.
OwnerRows is a sortable slice of string maps.
RoleRows is a sortable slice of string maps.
SchemataRows is a sortable slice of string maps.
SequenceRows is a sortable slice of string maps.
TableRows is a sortable slice of string maps.
TriggerRows is a sortable slice of string maps.
ViewRows is a sortable slice of string maps.