在PostgreSQL中以增量方式从源表(在db1中)到目标表(在db2中)获取所有数据

2024-10-02 08:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在AWS服务器端点中有两个PostgreSQL数据库db1=source databasedb2=destination database。对于db1,我只有读取权限,对于db2,我同时拥有读取和写入权限。db1是生产数据库有一个名为'公共采购'我的任务是从'公共采购'table indb1到'to be new created table'indb2(我将该表称为'public.u副本'). 每次我运行脚本来执行这个操作时,目标表public.u副本'indb2需要在不完全重新加载表的情况下进行更新。你知道吗

我的问题是怎样才能更有效地完成这项任务。我在网上做了很多研究,发现可以通过使用“psycopg2”模块将Python连接到PostgreSQL来实现。我对Python不是很精通,如果有人能帮我指出StackOverflow中的链接,在那里可以回答类似的问题,或者指导我可以做什么,如何实现,或者我可以参考的任何特定教程,那会有很大的帮助吗?提前谢谢。你知道吗

PostgreSQL版本:9.5, PostgreSQL GUI使用:pgadmin 3, 安装的Python版本:3.5


Tags: 版本aws数据库权限sourcepostgresql服务器端table
1条回答
网友
1楼 · 发布于 2024-10-02 08:29:27

虽然使用python可以做到这一点,但我建议您首先研究Postgres自己的模块Postgres\u fdw,如果您可以使用它的话:

The postgres_fdw module provides the foreign-data wrapper postgres_fdw, which can be used to access data stored in external PostgreSQL servers.

有关postgres docs的详细信息,特别是在设置之后,您可以:

Create a foreign table, using CREATE FOREIGN TABLE or IMPORT FOREIGN SCHEMA, for each remote table you want to access. The columns of the foreign table must match the referenced remote table. You can, however, use table and/or column names different from the remote table's, if you specify the correct remote names as options of the foreign table object.

Now you need only SELECT from a foreign table to access the data stored in its underlying remote table

为了简化设置,最好使用readonly db作为外部db。你知道吗

相关问题 更多 >

    热门问题