同步两个pg数据库

发布于 2024-08-02 08:03:31 字数 396 浏览 6 评论 0原文

我有一个 postgresql 服务器进程,分别在我的台式机和笔记本电脑上运行。

两台服务器都有一个名为 MG 的数据库,其方案/布局完全相同。现在,我将数据输入到类似的表中,但时间不同。

我通常将主键分开,这样它们就不会相互冲突。例如:笔记本电脑的奇数 pkey 和台式机的偶数 pkey。

现在如何干净地同步台式机和笔记本电脑之间的数据?

DESK:ADDRESS ----- LAP:ADDRESS
DESK:TO_DO   ----- LAP:TO_DO

DESK 使用像 1001 这样的 pkeys...用于插入
LAP 使用像 2001 这样的 pkeys... 对于插入,

我需要更新已修改的记录并插入新记录。但如何呢?

I have a postgresql server process each running in my desktop and the laptop.

Both servers have a database called MG with exactly same scheme/layout. Now I enter the data in to similar tables but at differing times.

I generally keep the primary keys separate so that they don't clash with each other. eg: oddnumber pkey for laptop and even number for desktop.

Now how do I synchronize the data between the desktop and laptop cleanly?

DESK:ADDRESS ----- LAP:ADDRESS
DESK:TO_DO   ----- LAP:TO_DO

DESK uses pkeys like 1001... for inserts
LAP uses pkeys like 2001... for inserts

I need both update for the modified records and insert for new records. But how?

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

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

发布评论

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

评论(5

鲜血染红嫁衣 2024-08-09 08:03:31

看起来 rubyrep 适合你。

It looks like rubyrep would work for you.

梦回梦里 2024-08-09 08:03:31

最简单的方法是编写自定义脚本/程序。这并不困难,而且您肯定会知道并理解它是如何工作的,因此扩展它将是微不足道的。

The simplest way would be to write a custom script/program. It's not difficult, and definitely you will know and understand how it works, so extending it will be trivial.

此刻的回忆 2024-08-09 08:03:31

使用 Slony-I 进行 postgreSQL 同步。

Use Slony-I to do postgreSQL synchronization.

八巷 2024-08-09 08:03:31

请参阅 PostgreSQL 文档,了解高可用性、负载平衡和复制

Please refer to PostgreSQL documentation for High Availability, Load Balancing, and Replication

爱你不解释 2024-08-09 08:03:31

为了添加到 user80168 的答案,这里是一个潜在的表合并:

package merge.tables;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class PostgreTableMerger {

    private static String dbOrigin = "jdbc:postgresql://server1:5432/DB";
    private static String dbDest = "jdbc:postgresql://server2:5432/DB";
    private static String tableToMerge = "important_results";

    public static void main(String[] args) throws Exception {

        Connection dbConnOrigin = DriverManager.getConnection(dbOrigin, "pgadmin", "pgadmin");
        Statement dbOriginStat = dbConnOrigin.createStatement();

        Connection dbConnDest = DriverManager.getConnection(dbDest, "pgadmin", "pgadmin");
        Statement dbDestStat = dbConnDest.createStatement();

        String sqlToExecute = "SELECT * FROM " + tableToMerge;
        ResultSet assets = dbOriginStat.executeQuery(sqlToExecute);
        ResultSetMetaData rsMeta = assets.getMetaData();


        while(assets.next()){
            String insertSQL  = "INSERT INTO " + tableToMerge + " VALUES(";

            for(int i = 1; i <= rsMeta.getColumnCount(); i++){
                String value = assets.getString(i);
                if(assets.wasNull()){
                    insertSQL += "NULL,";
                }else{
                    insertSQL += "'" + value + "',";
                }               
            }
            insertSQL =insertSQL.substring(0, insertSQL.length()-1) + ")";

            try{
                dbDestStat.executeUpdate(insertSQL);
            }catch(SQLException e){
                //TODO: attempt to update the row in the event of duplicate key
            }


        }
        return;
    }

}

To add to user80168's answer, here is a potential table merger:

package merge.tables;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class PostgreTableMerger {

    private static String dbOrigin = "jdbc:postgresql://server1:5432/DB";
    private static String dbDest = "jdbc:postgresql://server2:5432/DB";
    private static String tableToMerge = "important_results";

    public static void main(String[] args) throws Exception {

        Connection dbConnOrigin = DriverManager.getConnection(dbOrigin, "pgadmin", "pgadmin");
        Statement dbOriginStat = dbConnOrigin.createStatement();

        Connection dbConnDest = DriverManager.getConnection(dbDest, "pgadmin", "pgadmin");
        Statement dbDestStat = dbConnDest.createStatement();

        String sqlToExecute = "SELECT * FROM " + tableToMerge;
        ResultSet assets = dbOriginStat.executeQuery(sqlToExecute);
        ResultSetMetaData rsMeta = assets.getMetaData();


        while(assets.next()){
            String insertSQL  = "INSERT INTO " + tableToMerge + " VALUES(";

            for(int i = 1; i <= rsMeta.getColumnCount(); i++){
                String value = assets.getString(i);
                if(assets.wasNull()){
                    insertSQL += "NULL,";
                }else{
                    insertSQL += "'" + value + "',";
                }               
            }
            insertSQL =insertSQL.substring(0, insertSQL.length()-1) + ")";

            try{
                dbDestStat.executeUpdate(insertSQL);
            }catch(SQLException e){
                //TODO: attempt to update the row in the event of duplicate key
            }


        }
        return;
    }

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