MQTT Client subscribe to PostgreSQL DB Changes

耗尽温柔 提交于 2019-12-13 15:26:35

问题


I have a PostgreSQL DB that updates periodically, and want to publish the latest changes to a mosquitto broker. What is the most elegant way to do this approach ?


回答1:


Ok this idea is interesting i am using PostgreSQL 9.5 and in the console if i type CREATE LANGUAGE plpythonu; the python language is available. But how i can receive the whole database and the periodically changes of the database into my Broker.

CREATE FUNCTION publishChanges ()
  RETURNS integer
AS $$
  import paho.mqtt.client as mqtt
  import paho.mqtt.publish as publish
  #client = mqtt.Client()
  #client.connect("localhost", 1883, 60)

  publish.single("test/data", SELECT * from table ,hostname="127.0.0.1")

  #client.loop_forever()
$$ LANGUAGE plpythonu;



回答2:


Postgress appears to support python stored procedures which you should be able to call from a create/update trigger.

This combined with the paho python client would do what you want




回答3:


Postgress has its own publish/subscribe system: listen/notify. Set up the proper (listen)notify channels in postgress for the information you want published. The notify payload can be formatted as a mqtt message string. Create a client that listens to them and publishes to the mqtt-broker.



来源:https://stackoverflow.com/questions/36438729/mqtt-client-subscribe-to-postgresql-db-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!