PostgreSQL中如何比较两个数据库之间的数据?

发布于 2024-10-14 08:41:11 字数 65 浏览 2 评论 0原文

是否可以比较两个具有相同结构的数据库? 假设我有两个数据库 DB1 和 DB2,我想检查它们之间的数据是否存在差异。

Is it possible to compare two databases with identical structure?
Let say that I have two databases DB1 and DB2 and I want to check if there is a difference in data between them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

浪推晚风 2024-10-21 08:41:11

有几种工具:(

请注意,大多数工具只能比较结构,而不能比较数据)

免费工具:

商业:

There are several tools out there:

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

Free Ones:

Commercial:

套路撩心 2024-10-21 08:41:11

尝试在两个数据库上使用 pg_dump 并比较文件。

Try using pg_dump on both databases and diffing the files.

风柔一江水 2024-10-21 08:41:11

另一个免费应用程序(只能比较结构,但不能比较数据):

DBeaver - 您可以选择数据库、表格等进行相互比较

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

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

岁月蹉跎了容颜 2024-10-21 08:41:11

我正在为 Postgres 开发一个综合比较工具。测试期间它将是免费的。

PostgresCompare

最初这只是模式 (DDL) 比较,但我们可能也会扩展到数据。我相信这是许多商店需要的工具,以便从当前的 RDBMS 迁移,而不必改变他们的开发环境、操作等的工作方式。

I'm working on a comprehensive comparison tool for Postgres. It will be free while in beta.

PostgresCompare

Initially this is just schema (DDL) comparison but we will probably expand to data also. I believe this is a tool that a lot of shops require in order to move from their current RDBMS without having to also change how their development environments, operations etc work too.

⊕婉儿 2024-10-21 08:41:11

我评估了很多工具并找到了以下解决方案:

架构比较

最有趣的是 Liquibase、Persyas 和 PgCodeKeeper:(

问题)Liquebase 转换:

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

变成

BIGSERIAL

所以它是拒绝使用

问题)Persyas 工作正常,直到我添加了一些额外的架构,它开始抛出以下内容:

pyrseas_1       | TypeError: 'NoneType' object is not iterable

所以我找到了 PgCodeKeeper 它工作完美并且处于活动状态(您可以检查版本)。
我使用以下命令:

./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

数据比较:
我尝试过使用 Liquebase,但它不起作用,您可以在未回答的问题中看到我尝试的步骤 关于两个数据库与 Liquebase 的数据差异

所以我找到了另一个项目 SQL 工作台/J
它工作得非常好,并在 sql 中生成真正的差异。
我使用以下命令:

  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 ;'"

两个工具都支持对象过滤。真的很方便。

迁移

最后,我仅使用 Liquebase 进行迁移/版本跟踪。

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.

绿萝 2024-10-21 08:41:11

工具 pgtricks 有一个名为 pg_dump_splitsort 的命令,

您可以在转储上执行它。

示例:

pg_dump > pre-changes.sql
mkdir pre-changes
cd pre-changes
pg_dump_splitsort ../pre-changes.sql

now do some changes to your DB

pg_dump > post-changes.sql
mkdir post-changes
cd post-changes
pg_dump_splitsort ../post-changes.sql

cd ..

meld pre-changes post-changes

 meld-diff-of-directories

meld-diff-of-sql-dump

The tool pgtricks has a command called pg_dump_splitsort

You can execute it on a dump.

Example:

pg_dump > pre-changes.sql
mkdir pre-changes
cd pre-changes
pg_dump_splitsort ../pre-changes.sql

now do some changes to your DB

pg_dump > post-changes.sql
mkdir post-changes
cd post-changes
pg_dump_splitsort ../post-changes.sql

cd ..

meld pre-changes post-changes

meld-diff-of-directories

meld-diff-of-sql-dump

甜味超标? 2024-10-21 08:41:11

我见过的最好的工具
https://pythonhosted.org/Pyrseas/

  1. 从数据库 A 获取转储
    dbtoyaml ...

  2. 从 A 生成迁移 =>乙
    yamltodb ... [步骤 1 中生成的文件]

The best tool which I ever seen
https://pythonhosted.org/Pyrseas/

  1. Get dump from database A
    dbtoyaml ...

  2. Generate migration from A => B
    yamltodb ... [file generated in step 1]

旧人 2024-10-21 08:41:11

我创建了一个工具来比较 2 个实时 PostgreSQL 数据库(不是转储)、表数据和序列。虽然还很早,但已经实现了我想要的目标,也许它也可以帮助你。

https://github.com/dmarkey/pgdatadiff

I created a tool to compare 2 live PostgreSQL databases(not dumps), table data and sequences. Pretty early days but achieved what I wanted it to do, maybe it can help you too.

https://github.com/dmarkey/pgdatadiff

旧梦荧光笔 2024-10-21 08:41:11

还有另一个 GUI diff 工具,目前仅适用于 Windows:KS DB Merge Tools for PostgreSQL。不是开源,但功能相当强大的免费版本 - 允许比较模式、差异和合并表数据以及一些编程对象:

在此处输入图像描述

我是该工具的作者。

There is another GUI diff tool, currently it is for Windows only: KS DB Merge Tools for PostgreSQL. Not an open source, but pretty functional free version available - allows to compare schema, diff and merge table data and some programming objects:

enter image description here

I am the author of that tool.

旧伤慢歌 2024-10-21 08:41:11

我也在寻找一种工具来比较数据库中的数据(特别是我对比较 Redshift DB 感兴趣)。
到目前为止,我发现最好的是 https://www.dbbest.com/产品/数据库比较套件/#close
不幸的是,免费试用一天后到期。

I'm also looking for a tool to compare data in databases (in particular I was interested in comparing Redshift DB).
So far the best I found is https://www.dbbest.com/products/database-compare-suite/#close.
Unfortunately the free trial expires after one day.

成熟的代价 2024-10-21 08:41:11

在我看来,Dbforge是postgresql中最强大的数据比较工具。它是Devart公司的产品。你可以下载 此处

In my opinion Dbforge is the most powerful tool for comapring data in postgresql .It's a product of Devart company.you can download here.

生生漫 2024-10-21 08:41:11

对于简单数据库(没有触发器、存储过程和嵌套表)。

Use https://github.com/covrom/goerd for simple databases (without triggers, stored procs and nesting tables).

維他命╮ 2024-10-21 08:41:11

如果您已经拥有 JetBrains DataGrip 的许可证,则值得查看内置的比较功能进入该工具 - 请参阅有关该功能的供应商文档

一个不错的功能是您可以从要比较的两个表中选择任意行和列,然后在输出窗格中比较结果。这为您提供了另一种忽略诸如标识列之类的内容的方法,这些标识列在每个数据库中可能有不同的正当理由。

If you already own a license for JetBrains DataGrip, it is worth checking out the Compare feature that is built into that tool - see vendor documentation on the feature here.

One nice feature is you can select arbitrary rows and columns from the two tables you wish to compare, and then compare the results in the output pane. This gives you another way of ignoring things like identity columns which may have valid reasons to be different in each database.

旧人 2024-10-21 08:41:11

我发现这个漂亮的 PostgreSQL 编辑器也可以比较
https://github.com/finzaiko/zpg

I found this beautiful PostgreSQL editor also can compare
https://github.com/finzaiko/zpg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文