问题
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