PostgreSQL 数据库结构比对工具 apgdiff
简介
这个工具是通过比较pg_dump的备份文件进行的差异化对比,只能对比数据库结构的差异,也就是DDL层面的差异,无法对比数据。
安装使用
apgdiff是一个Java程序,所以,需要先安装Java
yum -y install java
wget https://codeload.github.com/fordfrog/apgdiff/tar.gz/refs/tags/release_2.7.0 -O apgdiff_2.7.0.tar.gz
tar -zxvf apgdiff_2.7.0.tar.gz
[pg13@lhrpgall soft]$ java -jar /soft/apgdiff-release_2.7.0/releases/apgdiff-2.7.0.jar -h
ERROR: Unknown option: -h
Usage: apgdiff [options] <old_dump> <new_dump>
Options:
--version:
displays apgdiff version
--add-defaults:
adds DEFAULT ... in case new column has NOT NULL constraint but no
default value (the default value is dropped later).
--add-transaction:
adds START TRANSACTION and COMMIT TRANSACTION to generated diff file.
--ignore-function-whitespace:
ignores multiple spaces and new lines when comparing content of
functions - WARNING: this may cause functions to appear to be
same in cases they are not, so use this feature only if you know
what you are doing.
--ignore-start-with:
ignores START WITH modifications on SEQUENCEs (default: do not ignore
these changes).
--ignore-schema-creation:
Removes the need of CREATE SCHEMA command on the input schemas
--in-charset-name <charset>:
charset that should be used for reading input files (standard charset
name supported by Java; the default is UTF-8).
--out-charset-name <charset>:
charset that should be used for writing output (standard charset name
supported by Java, default is UTF-8)
--output-ignored-statements:
outputs information about DDL statements that apgdiff ignores at this
moment (it will not output any SELECT, INSERT, UPDATE and/or DELETE
statements).
--ignore-slony-triggers:
when parsing SQL statements, ignores Slony triggers named
_slony_logtrigger and _slony_denyaccess.
--list-charsets
lists all supported charsets
演示示例
IP | Port | DB | |
数据库1 | 192.168.20.121 | 5432 | zc |
数据库2 | 192.168.20.121 | 5433 | zc |
- 创建测试数据
--数据库1
create table t1(id int)
INSERT INTO public.t1 (id) VALUES (1);
INSERT INTO public.t1 (id) VALUES (2);
INSERT INTO public.t1 (id) VALUES (3);
INSERT INTO public.t1 (id) VALUES (4);
INSERT INTO public.t1 (id) VALUES (5);
INSERT INTO public.t1 (id) VALUES (6);
--数据库2
CREATE TABLE public.t1 (
id integer,
name character varying(10)
INSERT INTO public.t1 (id, name) VALUES (11, NULL);
INSERT INTO public.t1 (id, name) VALUES (12, NULL);
INSERT INTO public.t1 (id, name) VALUES (13, NULL);
INSERT INTO public.t1 (id, name) VALUES (14, NULL);
INSERT INTO public.t1 (id, name) VALUES (15, NULL);
INSERT INTO public.t1 (id, name) VALUES (16, NULL);
CREATE TABLE public.t2 (
id integer,
name character(10)
CREATE SEQUENCE public.seq_id
START WITH 1
INCREMENT BY 2
NO MINVALUE
NO MAXVALUE
CACHE 1;
- pg_dump备份
/pg/12.8/app/bin/pg_dump -h 192.168.20.121 -U postgres --inserts --column-inserts -p 5432 -d zc -f pg12.dump
/pg/13.6/app/bin/pg_dump -h 192.168.20.121 -U postgres --inserts --column-inserts -p 5433 -d zc -f pg131.dump
- apgdiff对比
java -jar /soft/apgdiff-release_2.7.0/releases/apgdiff-2.7.0.jar --ignore-start-with pg12.dump pg131.dump > diff.sql
- 查看对比文件
[postgres@postgresql-121 ~]$ cat diff.sql
CREATE SEQUENCE seq_id
START WITH 1
INCREMENT BY 2
NO MAXVALUE
NO MINVALUE
CACHE 1;
CREATE TABLE t2 (
id integer,
name character(10)
ALTER TABLE t2 OWNER TO postgres;
ALTER TABLE t1
ADD COLUMN name character varying(10);
- 结论apgdiff工具会帮使用者生成一个包含对比文件中缺少的结构,但是数据的异同不会参数对比。
报错说明
pg_dump导出的过程中需要添加 --inserts --column-inserts 参数,否则会报如下错误
[postgres@postgresql-121 ~]$ java -jar /soft/apgdiff-release_2.7.0/releases/apgdiff-2.7.0.jar --ignore-start-with pg12.dump pg131.dump > diff.sql
Exception in thread "main" java.lang.RuntimeException: Cannot find ending semicolon of statement: 1