Save data to PostgreSQL

TIP

Support PostgreSQL 13 and below versions

Setup a PostgreSQL database, taking Mac OSX for instance:

  1. $ brew install postgresql
  2. $ brew services start postgresql
  3. ## create root user
  4. $ createuser --interactive --pwprompt
  5. Enter name of role to add: root
  6. Enter password for new role: public
  7. Enter it again: public
  8. Shall the new role be a superuser? (y/n) y
  9. ## create database named 'mqtt' using root
  10. $ createdb -U root mqtt
  11. $ psql -U root mqtt
  12. mqtt=> \dn;
  13. List of schemas
  14. Name | Owner
  15. --------+-------
  16. public | shawn
  17. (1 row)

Initiate PgSQL table:

  1. \$ psql -U root mqtt

create t_mqtt_msg table:

  1. CREATE TABLE t_mqtt_msg (
  2. id SERIAL primary key,
  3. msgid character varying(64),
  4. sender character varying(64),
  5. topic character varying(255),
  6. qos integer,
  7. retain integer,
  8. payload text,
  9. arrived timestamp without time zone
  10. );

Create a rule:

Go to EMQX DashboardSave data to PostgreSQL - 图1 (opens new window), select the “Rule” tab on the menu to the left.

Select “message.publish”, then type in the following SQL:

  1. SELECT
  2. *
  3. FROM
  4. "message.publish"

image

Bind an action:

Click on the “+ Add” button under “Action Handler”, and then select “Data to PostgreSQL” in the pop-up dialog window.

image

Fill in the parameters required by the action:

Two parameters is required by action “Data to PostgreSQL”:

1). SQL template. SQL template is the sql command you’d like to run when the action is triggered. In this example we’ll insert a message into pgsql, so type in the following sql template:

  1. insert into t_mqtt_msg(msgid, topic, qos, retain, payload, arrived) values (${id}, ${topic}, ${qos}, ${retain}, ${payload}, to_timestamp(${timestamp}::double precision /1000)) returning id

Before data is inserted into the table, placeholders like ${key} will be replaced by the corresponding values.

image

2). Bind a resource to the action. Since the dropdown list “Resource” is empty for now, we create a new resource by clicking on the “New Resource” to the top right, and then select “PostgreSQL”:

image

Select “PostgreSQL Resource”.

Configure the resource:

Set “PostgreSQL Database” to “mqtt”, “PostgreSQL User” to “root”, and keep all other configs as default, and click on the “Testing Connection” button to make sure the connection can be created successfully.

Finally click on the “Create” button.

image

Back to the “Actions” dialog, and then click on the “Confirm” button.

image

Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list:

image

We have finished, testing the rule by sending an MQTT message to emqx:

  1. > Topic: "t/1"
  2. >
  3. > QoS: 0
  4. >
  5. > Retained: false
  6. >
  7. > Payload: "hello1"

Then inspect the PgSQL table, verify a new record has been inserted:

image

And from the rule list, verify that the “Matched” column has increased to 1:

image