|
1 | 1 | # pgdiff - PostgreSQL schema diff
|
2 | 2 |
|
3 |
| -An important feature of this utility is that it never modifies any database directly. You are solely responsible for verifying the generated SQL *before* running it against your database. Now that you know about that, it should give you confidence that it is safe to try out and see what SQL gets generated. |
| 3 | +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. |
4 | 4 |
|
5 |
| -Written in GoLang, pgdiff compares the schema between two PostgreSQL databases and generates alter statements to be *manually* run against the second database. At the moment, not everything in the schema is compared, but the things considered important are: roles, sequences, tables, columns (and their default values), primary keys, unique constraints, foreign keys, roles, ownership information, and grants. |
| 5 | +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. |
| 6 | + |
| 7 | +pgdiff is written to be easy to expand and improve the accuracy of the diff. |
6 | 8 |
|
7 |
| -It is written to be easy to add and improve the accuracy of the diff. If you find something that seems wrong and you want me to look at it, please send me two schema-only database dumps that I can test with (Use the --schema-only option with pg\_dump) |
8 | 9 |
|
9 | 10 | ### download
|
10 |
| -[osx64](https://github.com/joncrlsn/pgrun/raw/master/bin-osx64/pgdiff "OSX 64-bit version") |
11 |
| -[osx32](https://github.com/joncrlsn/pgrun/raw/master/bin-osx32/pgdiff "OSX version") |
12 |
| -[linux64](https://github.com/joncrlsn/pgrun/raw/master/bin-linux64/pgdiff "Linux 64-bit version") |
13 |
| -[linux32](https://github.com/joncrlsn/pgrun/raw/master/bin-linux32/pgdiff "Linux version") |
14 |
| -[win64](https://github.com/joncrlsn/pgrun/raw/master/bin-win64/pgdiff.exe "Windows 64-bit version") |
15 |
| -[win32](https://github.com/joncrlsn/pgrun/raw/master/bin-win32/pgdiff.exe "Windows version") |
| 11 | +[osx](https://github.com/joncrlsn/pgrun/raw/master/bin-osx/pgdiff "OSX version") [linux](https://github.com/joncrlsn/pgrun/raw/master/bin-linux/pgdiff "Linux version") [windows](https://github.com/joncrlsn/pgrun/raw/master/bin-win/pgdiff.exe "Windows version") |
16 | 12 |
|
17 | 13 |
|
18 | 14 | ### usage
|
| 15 | + pgdiff [options] <schemaType> |
| 16 | + |
| 17 | + (where options are defined below and <schemaType> can be: ROLE, SEQUENCE, TABLE, COLUMN, INDEX, FUNCTION, VIEW, FOREIGN\_KEY, OWNER, GRANT\_RELATIONSHIP, GRANT\_ATTRIBUTE, TRIGGER) |
| 18 | + |
| 19 | +I've found that there is an ideal order for running the different schema types. This order should minimize the problems you encounter. For example, you'll 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". |
| 20 | + |
| 21 | +1. FUNCTION |
| 22 | +1. ROLE |
| 23 | +1. SEQUENCE |
| 24 | +1. TABLE |
| 25 | +1. COLUMN |
| 26 | +1. VIEW |
| 27 | +1. OWNER |
| 28 | +1. INDEX |
| 29 | +1. FOREIGN\_KEY |
| 30 | +1. GRANT\_RELATIONSHIP |
| 31 | +1. GRANT\_ATTRIBUTE |
| 32 | +1. TRIGGER |
| 33 | + |
| 34 | + |
| 35 | +### example |
| 36 | +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 don't copy that information. |
| 37 | + |
| 38 | +``` |
| 39 | +pgdiff -U dbuser -H localhost -D refDB -O 'sslmode=disable' \ |
| 40 | + -u dbuser -h localhost -d compDB -o 'sslmode=disable' \ |
| 41 | + TABLE |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +### options |
| 46 | + |
| 47 | +options | explanation |
| 48 | +----------------: | ------------------------------------ |
| 49 | + -V, --version | prints the version of pgdiff being used |
| 50 | + -?, --help | displays helpful usage information |
| 51 | + -U, --user1 | first postgres user |
| 52 | + -u, --user2 | second postgres user |
| 53 | + -W, --password1 | first db password |
| 54 | + -w, --password2 | second db password |
| 55 | + -H, --host1 | first db host. default is localhost |
| 56 | + -h, --host2 | second db host. default is localhost |
| 57 | + -P, --port1 | first db port number. default is 5432 |
| 58 | + -p, --port2 | second db port number. default is 5432 |
| 59 | + -D, --dbname1 | first db name |
| 60 | + -d, --dbname2 | second db name |
| 61 | + -O, --option1 | first db options. example: sslmode=disable |
| 62 | + -o, --option2 | second db options. example: sslmode=disable |
| 63 | + |
| 64 | + |
| 65 | +### getting started on linux and osx |
| 66 | +linux and osx binaries are packaged with an extra, optional bash script and pgrun program that helps speed the diffing process. |
| 67 | +1. download the tgz file for your OS |
| 68 | +1. untar it: ```tar -xzvf pgdiff.tgz``` |
| 69 | +1. cd to the new pgdiff directory |
| 70 | +1. edit the db connection defaults in pgdiff.sh |
| 71 | +1. ...or manually run pgdiff for each schema type listed in the usage section above |
| 72 | +1. review the SQL output for each schema type and, if you want to make them match, run it against db2 |
| 73 | + |
| 74 | + |
| 75 | +### getting started on windows |
| 76 | +1. download pgdiff.exe from the bin-win directory on github |
| 77 | +1. edit the db connection defaults in pgdiff.sh or... |
| 78 | +1. manually run pgdiff for each schema type listed in the usage section above |
| 79 | +1. review the SQL output and, if you want to make them match, run it against db2 |
19 | 80 |
|
20 |
| - pgdiff [database flags] <schemaType> |
21 | 81 |
|
| 82 | +### version history |
| 83 | +1. 0.9.0 - Implemented ROLE, SEQUENCE, TABLE, COLUMN, INDEX, FOREIGN\_KEY, OWNER, GRANT\_RELATIONSHIP, GRANT\_ATTRIBUTE |
| 84 | +1. 0.9.1 - Added VIEW, FUNCTION, and TRIGGER (Thank you, Shawn Carroll AKA SparkeyG) |
22 | 85 |
|
23 |
| - program flags | Explanation |
24 |
| --------------: | ------------------------------------ |
25 |
| - -U1 | first db postgres user |
26 |
| - -pw1 | first db password |
27 |
| - -h1 | first db host -- default is localhost |
28 |
| - -p1 | first db port number. defaults to 5432 |
29 |
| - -d1 | first db name |
30 |
| - -U2 | second db postgres user |
31 |
| - -pw2 | second db password |
32 |
| - -h2 | second db host -- default is localhost |
33 |
| - -p2 | second db port number. defaults to 5432 |
34 |
| - -d2 | second db name |
35 | 86 |
|
36 |
| -<schemaType> the type of objects in the schema to compare: ALL, ROLE, SEQUENCE, TABLE, COLUMN, INDEX, FOREIGN_KEY, OWNER, GRANT_RELATIONSHIP, GRANT_ATTRIBUTE |
| 87 | +### todo |
| 88 | +1. fix SQL for adding an array column |
| 89 | +1. add windows version of pgdiff.sh (or even better: re-write it in Go) |
| 90 | +1. allow editing of individual SQL lines after failure (this would probably be done in the script pgdiff.sh) |
| 91 | +1. store failed SQL statements in an error file for later fixing and rerunning? |
0 commit comments