且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有第二个数据库的PostgreSQL FOREIGN KEY

更新时间:2023-02-04 21:47:47

我没有机会亲自使用此功能,但您可能想研究外国数据包装器,它实际上是 dblink 的后继产品。尤其是 postgres-fdw

I've not had occasion to use this myself, but you might want to look into Foreign Data Wrappers, which are essentially the successor to dblink. In particular, postgres-fdw.

一旦完成了 fdw 的常规设置(上面链接中的第1-3步),您就可以创建外部表通过 CREATE FOREIGN TABLE (类似于远程数据库中的表进行定义),然后将该表用作外键 CONSTRAINT ,并查看其是否有效。

Once the general setup of the fdw is in place (steps 1-3 in the link above), you could create a foreign table via CREATE FOREIGN TABLE, defined like the table in your remote DB, and then use that table as part of the foreign key CONSTRAINT, and see if it works.

如果这不起作用,另一种选择是让一个过程使用ETL作为数据(例如,通过 Python 脚本)从远程服务器到本地服务器(例如,每小时或每天一次,具体取决于大小),然后您将拥有一个真正的本地表在外键 CONSTRAINT 中使用。它不是实时的,但是根据您的需要,可能就足够了。

If that doesn't work, another option would be to have a process which ETL's the data (say, via a Python script) from the remote server over to the local server (say, on an hourly or daily basis, depending on the size), and then you would have a true local table to use in the foreign key CONSTRAINT. It wouldn't be real-time, but depending on your needs, may suffice.