AndreyM
AndreyM

Reputation: 119

How to merge two database copies (postgresql)

I have two versions of the same database. One is current and the other one has been restored from a backup. Now I need to merge them to basically move the data from the backup into the current database preserving the relations between different tables.

Are there any tools that may help to accomplish this?

Upvotes: 3

Views: 1687

Answers (1)

Tim
Tim

Reputation: 5369

Rubyrep has a sync feature that may do what you want.

Rubyrep is a trigger based async replication system for postgresql and mysql. But one of it's features is a "sync" command, where it scans the tables for differences and tries to merge them together. Normally you woud use sync first, then use replicate to keep the two databases in sync after that, but there's no reason you can't just use the "sync" command alone.

I believe it uses the primary keys to compare data, so depending on your schema, it may or may not work for you. You probably want to make a backup of your data first.

I think it's going to merge records with the same primary key. You can tell it which side wins. But if you have two records with the same primary key that shouldn't be merged, I think you will run into trouble.

Still, it might work if you help it. You might be able to tell it, e.g. "username" is the primary key instead of "id". Or manually adjust your serial primary keys ahead of time, if the data is non-overlapping.

Upvotes: 4

Related Questions