PostgreSQL Integration
How to integrate PostgreSQL with Connectware, including writing shop floor data into a production events table and polling query results back into MQTT topics.
This guide describes how to integrate PostgreSQL with Connectware. You configure a service commissioning file that writes shop floor data into a PostgreSQL table and polls query results back into an MQTT topic. A complete example file is available at the end of this guide.
Objectives
Establishing a connection between Connectware and PostgreSQL.
Writing shop floor data from an ISA-95-style topic hierarchy into a production events table.
Polling recent rows from the database into an MQTT topic on a fixed interval.
Prerequisites
To follow this guide, you will need the following:
A running instance of Cybus Connectware.
A PostgreSQL server that is reachable from Connectware.
A database role with
INSERTandSELECTprivileges on the target database.Access to the Admin UI with sufficient user permissions.
Basic knowledge of MQTT and the Connectware services concept (for example, service commissioning files, connections, and endpoints).
Connectware and PostgreSQL Integration
Connectware communicates with PostgreSQL through the SQL connector. The connector works by defining SQL queries or query templates on endpoints:
Write endpoints define a query template, typically an
INSERTstatement. The template contains placeholders in the form$identifier, which the connector replaces with the values from the JSON payload of each incoming MQTT message (see Placeholder Syntax).Subscribe endpoints define a query and a polling interval. The connector executes the query on a regular basis and publishes the result rows as a JSON array to the MQTT broker.
The same connector also serves MariaDB databases; only the URL scheme in the connection changes.
The MQTT topics in this guide follow an ISA-95-style equipment hierarchy (<enterprise>/<site>/<area>/<line>/<cell>). The mapping subscribes with named wildcards across all levels, so any machine in the hierarchy is picked up without changing the integration, and the topic levels become column values in the database.
Production Events Table
The integration writes into a single table that stores one row per shop floor event. Create it in your target database:
The event_time column defaults to the insert time, stored as timestamp with time zone, so the messages do not need to carry a timestamp. If you want to store machine timestamps instead, add a column for them and extend the INSERT statement with another placeholder.
PostgreSQL Connection Properties
We add the connection values as parameters to the service commissioning file, so you can set them when you install the service.
Do not worry about copying the service commissioning file snippets together into one, the complete example file is available at the end of this guide.
postgresHostandpostgresPort: The hostname and port of your PostgreSQL server. The default port is5432.postgresUsernameandpostgresPassword: The database role that Connectware uses to connect.postgresDatabase: The database that contains theproduction_eventstable.pollingInterval: The interval in milliseconds between polling queries.topicRoot: The root of the MQTT topic hierarchy. Defaults toenterprise.
PostgreSQL Connection
To connect to the database, we set up a Cybus::Connection resource that uses the SQL connector. The connector expects a single connection URL of the form postgres://<user>:<password>@<host>:<port>/<database>, which the !sub substitution assembles from the parameters. All endpoints in this guide share this connection.
For all available connection properties, including certificate handling and the reconnection strategy, see SQL Connection Properties.
Writing Production Events
The write endpoint defines the INSERT statement as a query template. Each placeholder, for example $site, is replaced with the value of the matching key in the JSON payload of the incoming message.
The mapping feeds the endpoint from the topic hierarchy. It uses named wildcards, so the topic levels are available in the $context.vars object of the transform rule. The rule builds one flat object whose keys match the query placeholders, combining the topic levels with the event_type and value fields of the machine payload.
Any message published to a matching topic, for example enterprise/hamburg/assembly/line-1/press-01/production-events, now inserts one row into the table. The machine payload only needs the event fields, the topic provides the rest:
All placeholders defined in the query must exist in the resulting message payload. If one is missing, the connector logs an error and ignores the message (see Writing Data).
The SQL connection on the Connectware side does not perform any data validation against the database schema. The senders of the MQTT messages must ensure that the values match the column types, for example a number for the event_value column.
Polling Recent Production Events
For the opposite direction, a subscribe endpoint executes a query on a fixed interval and publishes the result rows to the MQTT broker. This example reads the production events of the last five minutes, so downstream consumers such as dashboards or MES systems receive a regularly updated snapshot.
Choose the polling interval carefully. A low value can overload the database. Instead of a fixed interval, you can also poll on a schedule with the cronExpression property (see SQL Endpoint Properties).
Every query execution publishes a message with a timestamp property and a value property that contains the result rows. If the query returns no rows, value is an empty array ([]).
Verifying the Integration
Install the service and set the parameters with the values of your PostgreSQL server.
Check that the connection is in the Connected state on the service details page in the Admin UI. If the credentials or the database name are wrong, the connection does not reach the connected state.
Publish a test message with the machine payload shown in this guide to
enterprise/hamburg/assembly/line-1/press-01/production-events, for example with an MQTT client or the Admin UI.Check that the row arrived in the database, for example with
SELECT * FROM production_events ORDER BY id DESC LIMIT 10in psql. The result of every write is also published to the/restopic of the write endpoint, withvalueset totrueon success.Use the Data Explorer to inspect the polled result rows on the
enterprise/production-events/recenttopic.
Service Commissioning File Example
Last updated
Was this helpful?

