> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL Connector

> Store MQTT data in PostgreSQL and PostgreSQL-compatible databases with automatic query execution and SSL support

## PostgreSQL Overview

The `POSTGRESQL` connector stores MQTT messages in PostgreSQL databases. It supports SSL connections, parameterized queries, and automatic reconnection for reliable data persistence.

Because the connector speaks the **PostgreSQL wire protocol**, it also works with any service built on PostgreSQL — you use the same `POSTGRESQL` connector type and point `SQL_CONFIG` at that provider's host, port, database, and credentials.

| Platform | Typical use |
| - | - |
| **PostgreSQL** | Self-hosted, on-premises, or VM deployments |
| **Tiger Data** | Managed Postgres with TimescaleDB (Tiger Cloud) for time-series and IoT workloads |
| **TimescaleDB** | PostgreSQL extension for hypertables and time-series SQL (self-hosted or on Tiger Data) |
| **Amazon RDS / Aurora PostgreSQL** | AWS-managed relational storage |
| **Azure Database for PostgreSQL** | Microsoft Azure managed Postgres |
| **Google Cloud SQL for PostgreSQL** | GCP managed Postgres |
| **Supabase** | Managed Postgres with realtime APIs, often used for app and dashboard backends |

> [!NOTE]
>
> PostgreSQL is ideal for structured data that requires complex queries, joins, and ACID compliance. Use it when you need relational integrity and powerful SQL capabilities.

> [!TIP]
>
> For **TimescaleDB** or **Tiger Data** (Tiger Cloud), use standard PostgreSQL connection settings — host, port `5432`, database, user, and password from your service dashboard. Create hypertables and time-series schemas in SQL as you would in any Postgres client; the connector executes your `QUERY` strings against that database.

## Basic Syntax

```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR SensorDB WITH TYPE POSTGRESQL
    ADD SQL_CONFIG
        WITH SERVER "postgres.example.com"
        WITH PORT '5432'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT StoreSensorReading
        WITH SOURCE_TOPIC "sensors/+/reading"
        WITH QUERY "INSERT INTO readings (ts, sensor_id, value) VALUES (NOW(), '{sensor_id}', '{value.json}')"
```

***

## Connection Configuration

### SQL\_CONFIG Parameters

**SERVER** `string` *required*

PostgreSQL server hostname or IP address.

**DATABASE** `string` *required*

Target database name.

**USERNAME** `string` *required*

Database username.

**PASSWORD** `string` *required*

Database password. Characters such as `;`, `=`, and quotes work as typed, here and in `USERNAME` and `DATABASE`.

> [accordion] Optional Settings
>
> **PORT** `integer`
>
> PostgreSQL port. Default: 5432.

> [accordion] SSL/Security
>
> **USE\_SSL** `boolean`
>
> Enable SSL connection. Default: false.
>
> **TRUST\_SERVER\_CERTIFICATE** `boolean`
>
> Trust server certificate without validation. Default: false.

> [accordion] Timeouts
>
> **CONNECTION\_TIMEOUT** `integer`
>
> Connection timeout in seconds. Default: 30.
>
> **COMMAND\_TIMEOUT** `integer`
>
> Command timeout in seconds. Default: 30.

> [accordion] Pool and writes
>
> **MAX\_POOL\_SIZE** `integer`
>
> Maximum connections in the pool. Passed to the PostgreSQL driver. Omit to keep the driver default.
>
> **MIN\_POOL\_SIZE** `integer`
>
> Minimum connections in the pool. Passed to the PostgreSQL driver. Omit to keep the driver default. Must be less than or equal to `MAX_POOL_SIZE`.
>
> **CONNECTION\_IDLE\_LIFETIME** `integer`
>
> How long an idle pooled connection may be kept. Passed to the PostgreSQL driver. Omit to keep the driver default.
>
> **CONNECTION\_LIFETIME** `integer`
>
> How long a pooled connection may be kept. Passed to the PostgreSQL driver. Omit to keep the driver default.
>
> **WRITE\_CONCURRENCY** `integer`
>
> How many MQTT-triggered SQL events may run at once. Alias: `MAX_CONCURRENT_WRITES`. Optional.
>
> **WRITE\_QUEUE\_FULL\_POLICY** `string` (default: block)
>
> What to do when MQTT-triggered writes are already at `WRITE_CONCURRENCY`. `block` waits, and `WRITE_QUEUE_CAPACITY` is ignored. `drop_oldest` and `drop_newest` apply `WRITE_QUEUE_CAPACITY`. Dropping an event logs a Warning and counts the drop.
>
> **WRITE\_QUEUE\_CAPACITY** `integer`
>
> How many waiting MQTT-triggered events to keep when the policy is `drop_oldest` or `drop_newest`. Ignored when the policy is `block`.

Invalid pool values are refused. `MIN_POOL_SIZE` must be less than or equal to `MAX_POOL_SIZE`.

A failed query does not mark the connector Disconnected. A constraint violation, a cast error, PostgreSQL `53300` (`too_many_connections`), an exhausted pool, and any other query failure leave it connected. Only a transport or connection failure marks it Disconnected, so one bad row does not stop later telemetry.

> [!WARNING]
>
> **Never hardcode credentials in production.** Use environment variables and encrypted secrets to keep sensitive values out of your connector definitions:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> WITH SERVER GET ENV "DB_HOST"
> WITH USERNAME GET ENV "DB_USER"
> WITH PASSWORD GET SECRET "DB_PASSWORD"
> ```
>
> See [Environment Variables & Secrets](https://docs.coreflux.org/latest/mqtt-broker/secrets-and-env.md) for setup and usage.

### Reconnecting after a dropped connection

When the database ends the connector's sessions, the connection drops, or a query runs past `COMMAND_TIMEOUT`, the connector is marked Disconnected. The next event reconnects it, straight away and then at most every 5 seconds until the database answers, and runs once the connection is back. No redeploy is needed. The event that was running on the dropped session fails and is not retried.

***

## Agent queries

An agent can read this database when the connector opts in and the agent grants `connector_query`. The agent runs one read statement. It cannot insert, update, or delete. See [Database queries](https://docs.coreflux.org/latest/lot-language/agents/syntax.md#database-queries).

| Clause | Default | Effect |
| - | - | - |
| `AGENT_QUERIES` | `"off"` | `"off"` or `"read"`. `"write"` is refused |
| `AGENT_USERNAME` / `AGENT_PASSWORD` | Connector login, with a warning | Login the agent uses. Prefer a read-only user |
| `AGENT_CONNECTION_STRING` | — | Alternative to the agent user and password |
| `AGENT_MAX_ROWS` | `200` | Most rows returned to the model |

If you omit the agent login, PostgreSQL uses the connector user inside a read-only transaction and rolls it back. The broker logs a warning. Give the agent its own read-only user.

The person who asks and the agent's `DEFAULT_USER` both need `PublishSys` on `$SYS/Coreflux/Routes/<connector>/Query`.

```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR PlantDB WITH TYPE POSTGRESQL
    ADD SQL_CONFIG
        WITH SERVER GET ENV "PG_HOST"
        WITH PORT 5432
        WITH DATABASE "plant"
        WITH USERNAME GET ENV "PG_USER"
        WITH PASSWORD GET SECRET "PG_PASSWORD"
        WITH AGENT_QUERIES "read"
        WITH AGENT_USERNAME GET ENV "PG_AGENT_USER"
        WITH AGENT_PASSWORD GET SECRET "PG_AGENT_PASSWORD"
        WITH AGENT_MAX_ROWS 200
```

***

## Event Configuration

### EVENT Parameters

**SOURCE\_TOPIC** `string` *required*

MQTT topic pattern that triggers the INSERT. Supports wildcards.

**QUERY** `string` *required*

SQL INSERT statement with placeholders for dynamic values.

> [accordion] Optional Settings
>
> **DESTINATION\_TOPIC** `string`
>
> MQTT topic to publish query result status.

### Query Placeholders

| Placeholder | Description |
| - | - |
| `{value.json}` | Full JSON payload |
| `{value.json.field}` | Specific field from JSON |
| `{timestamp}` | Message timestamp |
| `{source_topic}` | Original MQTT topic |
| `{field}` | Field extracted from topic path |

***

## How placeholders are quoted

The broker places each placeholder in an event `QUERY` by the quotes around it. A publisher on the source topic can no longer change the SQL by putting SQL in the payload.

> [!WARNING]
>
> **Action:** Put every placeholder that carries text in single quotes (`WHERE status = '{payload}'`). Put a placeholder that names a table or column in double quotes (`FROM "{topic.2}"`). Quoted identifiers are case-sensitive: match the table's stored case. An unquoted placeholder must be a number, a comma-separated list of numbers, `true`, `false`, or `null`. Anything else, including a bare name such as `line_1`, refuses the event. The message names the placeholder.

| Where the placeholder sits | What happens to the value |
| - | - |
| Inside single quotes: `'{payload}'` | Quotes in the value are doubled. Inside a PostgreSQL `E'…'` string, backslashes are escaped as well. |
| Inside double quotes: `"{topic.2}"` | Double quotes in the value are doubled. Use this for a table or column name. |
| Inside a dollar-quoted string: `$$…{payload}…$$` | A value that contains `$` refuses the event. |
| Unquoted: `VALUES ({value})` | Only a number, a comma-separated list of numbers, `true`, `false`, or `null`. |
| In a SQL comment | Left as written. |

`{env.NAME}` and `{secret.NAME}` are placed as written. A value that itself contains `{topic}` is stored as text and is not expanded again. The same rules on the other SQL engines are in [How placeholders are quoted](https://docs.coreflux.org/latest/connectors/data-storage/overview#how-placeholders-are-quoted). MongoDB and OpenSearch JSON queries place each value as one JSON value instead (see [OpenSearch placeholders](https://docs.coreflux.org/latest/connectors/data-storage/opensearch.md#placeholders)).

***

## Writing Data

> [tab] Basic Sensor Storage
>
> Store temperature readings with timestamps:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR TemperatureDB WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>     ADD EVENT StoreTemperature
>         WITH SOURCE_TOPIC "sensors/+/temperature"
>         WITH DESTINATION_TOPIC "db/status/temperature"
>         WITH QUERY "INSERT INTO temperature_readings (recorded_at, sensor_id, value, unit) VALUES (NOW(), '{sensor_id}', {value.json.value}, '{value.json.unit}')"
> ```
>
> **Input at** `sensors/temp001/temperature`:
>
> ```json wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> {"value": 23.5, "unit": "celsius"}
> ```
>
> `{value.json.value}` stays unquoted because `23.5` is a number. If that field is text, put it in single quotes (`'{value.json.value}'`), or the event is refused.

> [tab] With SSL
>
> Secure connection with SSL:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR SecureDB WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.cloud.com"
>         WITH PORT '5432'
>         WITH DATABASE "production"
>         WITH USERNAME "app_user"
>         WITH PASSWORD "secure_password"
>         WITH USE_SSL "true"
>         WITH TRUST_SERVER_CERTIFICATE "false"
>     ADD EVENT StoreEvent
>         WITH SOURCE_TOPIC "events/#"
>         WITH QUERY "INSERT INTO events (ts, topic, payload) VALUES (NOW(), '{source_topic}', '{value.json}')"
> ```

> [tab] Multiple Events
>
> Different tables for different data types:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR MultiTableDB WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>
>     ADD EVENT StoreSensorData
>         WITH SOURCE_TOPIC "sensors/+/data"
>         WITH QUERY "INSERT INTO sensor_data (ts, sensor_id, value) VALUES (NOW(), '{sensor_id}', '{value.json}')"
>
>     ADD EVENT StoreAlerts
>         WITH SOURCE_TOPIC "alerts/+"
>         WITH QUERY "INSERT INTO alerts (ts, alert_type, message) VALUES (NOW(), '{alert_type}', '{value.json}')"
>
>     ADD EVENT StoreMetrics
>         WITH SOURCE_TOPIC "metrics/#"
>         WITH QUERY "INSERT INTO metrics (ts, topic, data) VALUES (NOW(), '{source_topic}', '{value.json}')"
> ```

> [tab] JSON Column
>
> Store full payload in JSONB column:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR JSONStorage WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>     ADD EVENT StoreJSON
>         WITH SOURCE_TOPIC "devices/#"
>         WITH QUERY "INSERT INTO device_data (ts, topic, payload) VALUES (NOW(), '{source_topic}', '{value.json}'::jsonb)"
> ```
>
> This allows powerful JSON queries in PostgreSQL:
>
> ```sql wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> SELECT * FROM device_data WHERE payload->>'temperature' > '25';
> ```

> [tab] UPSERT
>
> Insert a record, or update it if a conflict on the unique key occurs. Useful for device registration or status tables where duplicate messages are possible:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR DeviceRegistry WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>     ADD EVENT RegisterDevice
>         WITH SOURCE_TOPIC "devices/+/registered"
>         WITH QUERY "INSERT INTO devices (device_id, name, area, active, updated_at) VALUES ('{value.json.device_id}', '{value.json.name}', '{value.json.area}', TRUE, NOW()) ON CONFLICT (device_id) DO UPDATE SET name = EXCLUDED.name, area = EXCLUDED.area, updated_at = NOW()"
> ```
>
> `ON CONFLICT (device_id) DO UPDATE SET` updates only the changed columns when the device already exists — the row is never duplicated.

> [tab] UPDATE
>
> Modify existing records when a status change or update arrives:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR DeviceRegistry WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>     ADD EVENT UpdateDeviceStatus
>         WITH SOURCE_TOPIC "devices/+/status"
>         WITH QUERY "UPDATE devices SET current_status = '{value.json.status}', last_seen = NOW() WHERE device_id = '{value.json.device_id}'"
> ```
>
> Only rows matching the `WHERE` clause are modified. Unmatched messages produce no change — no error is raised.

> [tab] DELETE
>
> Remove records when a decommission or cleanup event arrives:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE CONNECTOR DeviceRegistry WITH TYPE POSTGRESQL
>     ADD SQL_CONFIG
>         WITH SERVER "postgres.example.com"
>         WITH PORT '5432'
>         WITH DATABASE "iot_data"
>         WITH USERNAME "iot_user"
>         WITH PASSWORD "secure_password"
>     ADD EVENT DecommissionDevice
>         WITH SOURCE_TOPIC "devices/+/decommissioned"
>         WITH QUERY "DELETE FROM devices WHERE device_id = '{value.json.device_id}' AND active = FALSE"
> ```
>
> The `AND active = FALSE` guard prevents accidental deletes if a message arrives before the device is properly deactivated.

> [!NOTE]
>
> **Alternative: STORE IN with Models** — Instead of writing EVENT queries, you can bind a [model](https://docs.coreflux.org/latest/lot-language/models/overview) directly to this connector. Every `PUBLISH MODEL` call automatically inserts a row — no query needed:
>
> ```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
> DEFINE MODEL SensorReading
>     ADD STRING "sensor_id"
>     ADD DOUBLE "value"
>     STORE IN "SensorDB"
>         WITH TABLE "sensor_readings"
> ```
>
> See [Data Storage Overview](https://docs.coreflux.org/latest/connectors/data-storage/overview#with-models-store-in) for the full STORE IN workflow.

***

## Reading Data

EVENTs also support SELECT queries to read data back from the database. Publish a message to the event's `SOURCE_TOPIC`, and the query result is published to `DESTINATION_TOPIC`:

```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR SensorDB WITH TYPE POSTGRESQL
    ADD SQL_CONFIG
        WITH SERVER "postgres.example.com"
        WITH PORT '5432'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT GetLatestReading
        WITH SOURCE_TOPIC "db/query/latest"
        WITH DESTINATION_TOPIC "db/result/latest"
        WITH QUERY "SELECT sensor_id, value FROM readings WHERE sensor_id = '{payload}' ORDER BY ts DESC LIMIT 1"
```

To trigger this query, publish the sensor ID to the source topic:

```
Topic:   db/query/latest
Payload: temp001
```

The query result is published to `db/result/latest`, where your actions or external clients can consume it.

### Timed Polling

EVENTs can also run SELECT queries on a schedule instead of waiting for an MQTT trigger. Use `WITH EVERY` to publish fresh data at a fixed interval:

```lot wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
DEFINE CONNECTOR DeviceRegistry WITH TYPE POSTGRESQL
    ADD SQL_CONFIG
        WITH SERVER "postgres.example.com"
        WITH PORT '5432'
        WITH DATABASE "iot_data"
        WITH USERNAME "iot_user"
        WITH PASSWORD "secure_password"
    ADD EVENT PollActiveDevices
        WITH EVERY 10 SECONDS
        WITH DESTINATION_TOPIC "dashboard/devices/active"
        WITH QUERY "SELECT device_id, name, current_status, last_seen FROM devices WHERE active = TRUE ORDER BY name"
```

The query result is published to `dashboard/devices/active` every 10 seconds — no trigger message needed.

***

## Table Schema Examples

Create tables to match your data:

```sql wrap theme={"theme":{"light":"css-variables","dark":"css-variables"},"languages":{"custom":["/languages/lot.json"]}}
-- Basic sensor readings
CREATE TABLE sensor_readings (
    id SERIAL PRIMARY KEY,
    recorded_at TIMESTAMP DEFAULT NOW(),
    sensor_id VARCHAR(50),
    value DOUBLE PRECISION,
    unit VARCHAR(20)
);

-- Full JSON storage
CREATE TABLE device_data (
    id SERIAL PRIMARY KEY,
    ts TIMESTAMP DEFAULT NOW(),
    topic VARCHAR(255),
    payload JSONB
);

-- Alerts table
CREATE TABLE alerts (
    id SERIAL PRIMARY KEY,
    ts TIMESTAMP DEFAULT NOW(),
    alert_type VARCHAR(50),
    message TEXT
);
```

***

## Troubleshooting

> [accordion] Connection Refused
>
> * Verify SERVER and PORT are correct
> * Check PostgreSQL is running and accepting connections
> * Verify firewall allows connections on port 5432
> * Check `pg_hba.conf` allows your IP address

> [accordion] Authentication Failed
>
> * Verify USERNAME and PASSWORD are correct
> * Check user has permissions on the DATABASE
> * Verify authentication method in `pg_hba.conf`

> [accordion] SSL Connection Errors
>
> * Ensure USE\_SSL matches server requirements
> * For self-signed certs, set TRUST\_SERVER\_CERTIFICATE "true"
> * Verify SSL is enabled on PostgreSQL server

> [accordion] Query Syntax Errors
>
> * Check placeholder names match topic structure
> * Put text placeholders in single quotes, and table or column placeholders in double quotes. An unquoted text value refuses the event. See [How placeholders are quoted](#how-placeholders-are-quoted)
> * Verify table and column names exist
> * Test query manually in psql first

> [accordion] Timeout Errors
>
> * Increase CONNECTION\_TIMEOUT for slow networks
> * Increase COMMAND\_TIMEOUT for complex queries
> * Check server load and query performance

***

## Next Steps

> [card] [Data Storage Connectors Overview](https://docs.coreflux.org/latest/connectors/data-storage/overview)
>
> Compare all storage options.

> [card] [MySQL Connector](https://docs.coreflux.org/latest/connectors/data-storage/mysql.md)
>
> Configure MySQL database storage.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.