Publish to PostgreSQL
PostgreSQL publishing writes TDengine TMQ subscription data to a single PostgreSQL table for downstream applications to query. Verification must confirm that primary keys, timestamps, and business fields in the target match the source. Target connectivity, a successful preview, or healthy task status does not mean verification is complete.
Where to operate: taosExplorer → Data Out → PostgreSQL. Prepare the source in the taosExplorer Data Browser or taos CLI, and prepare the target database and verify downstream in a PostgreSQL client.
Data flows TDengine TMQ → taosX → PostgreSQL. To ingest data from PostgreSQL, follow PostgreSQL.
1. Prepare the Environment
- Verify that
taosdandtaosAdapterare running and that taosX is installed with data publishing support. - Use PostgreSQL 12 and later. The target database and schema must already exist. taosX does not create either.
- Set
WAL_RETENTION_PERIODon the source database to a value greater than 0. The source user needs permission to create topics and subscribe. The target user needs permission to write to the table and, if automatic table creation is enabled, to create it. - Execution through taosX-Agent or
VIAis not supported.
2. Prepare Data
Run the following SQL in the taos CLI or taosExplorer to create an example supertable and insert a row. You do not need to create a topic manually.
CREATE DATABASE IF NOT EXISTS dataout_demo VGROUPS 1 WAL_RETENTION_PERIOD 3600;
CREATE STABLE IF NOT EXISTS dataout_demo.meters (
ts TIMESTAMP,
current FLOAT,
voltage INT
) TAGS (site INT);
INSERT INTO dataout_demo.meter01 USING dataout_demo.meters TAGS (1)
VALUES (NOW, 10.5, 220);
3. Create a PostgreSQL Data Out Task
In taosExplorer, open Data Out, click Create New Task, enter a task name, and select PostgreSQL as the data target type.
3.1 TDengine Subscription

- TDengine Endpoint: for example
tmq+ws://USER:PASSWORD@localhost:6041/dataout_demo. The database in the path is the default database of the subscription SQL. - Subscription SQL: a single
SELECTquery. It follows TDengine query topic rules: query one table, child table or supertable, and project columns, tags,tbnameand scalar functions with an optionalWHERE. Aggregates, windows, JOIN, subqueries,UNION,DISTINCT,GROUP BY,ORDER BY,LIMITand virtual tables are not supported. - Start Position:
earliestincludes historical data,latestsubscribes only new data. It takes effect only on the first consumption of the consumer group. - Consumer Group ID: defaults to the task topic name. Changing it uses a new consumer group and rereads the data.
- TSDB Data: when enabled, persisted TSDB data is subscribed as well, not only WAL data. Together with the start position it determines what is read on the first start.
| Start Position | TSDB Data | Data read on the first start |
|---|---|---|
| earliest | on | All persisted historical data from TSDB, then WAL for new data |
| earliest | off | Only from the earliest position still retained in WAL |
| latest | any | Only data written after the task starts |
For this example, use SELECT ts, tbname, current, voltage FROM dataout_demo.meters. For more about query topics and consumer positions, see Data Subscription.
3.2 Connection and Authentication
Under Connection, enter the host, port (default 5432), and the name of an existing database such as rdb_out. Under Authentication, enter the database user and password.
- Application Name: Optional name for identifying the connection in PostgreSQL.
- SSL Mode: Choose
DISABLE,ALLOW,PREFER, orREQUIREto match the PostgreSQL server’s TLS configuration.
3.3 Target Table

- Schema: The schema of the target table. Defaults to
public. - Table: Enter the target table name, such as
meters_out. - Create Table: If enabled, taosX creates the table when it does not exist. For an existing table, taosX checks compatibility without changing the table definition.
- Primary Key: Fetch the result columns and select up to 16 columns. The form initially selects the first eligible column. Select
tsandtbnameas the composite key in this example. If you select onlyts, rows from different child tables at the same timestamp can overwrite one another. - Timestamp Storage: With Auto, millisecond and microsecond timestamps use a date-time type and nanosecond timestamps use
BIGINT. WithBIGINT, all timestamps are stored as raw integers.
3.4 Advanced
- Batch Size: maximum rows written per batch, default 1000.
- Batch Timeout (ms): maximum wait before a batch is written, default 1000.
- Write Timeout and Max Retry Time: configurable through DSN parameters, default to 60 and 180 seconds.
3.5 Connectivity Check and Preview
- Click Check Connection to check that the query returns a compatible result schema and that the target is reachable. This check does not create a topic. For query-topic restrictions, use the full preview or start the task.
- Set Row Count and Wait Time (s) and click Preview Data. The preview returns when it reaches either limit. Review the sample rows, the table creation statement (if a table needs to be created), and the write statement template. Preview does not write to the target database or alter the task’s consumer position.
- Click Submit, find the original task in the Data Out task list, and confirm its actual running status. Start the original task if it is stopped; do not create a duplicate. Then verify the target table as described in section 4.
4. Verify the Target Table
Confirm the target database and table in a PostgreSQL client. Then run the following in the source cluster's taosExplorer Data Browser or taos CLI:
INSERT INTO dataout_demo.meter01 VALUES (NOW, 11.5, 221);
SELECT ts, tbname, current, voltage FROM dataout_demo.meters ORDER BY ts DESC LIMIT 5;
Record the actual ts and tbname of the new row, then query the target table in a PostgreSQL client:
Connect to the target database rdb_out before running this query:
SELECT ts, tbname, current, voltage
FROM public.meters_out
ORDER BY ts DESC LIMIT 5;
Find the row by its recorded primary key. Confirm current=11.5 and voltage=221, and compare timestamps according to the storage mode, precision, and time zone. Write another row at a new timestamp to confirm continuous delivery. Do not rely only on total row counts or task counters.
Connectivity checks and previews do not replace verification of actual writes. Preview does not write to the target database or alter the task's consumer position. Submit and start the task, then query the target table to confirm the result.
5. DSN Example
The following source and target DSN fragments belong in a task definition; they are not standalone SQL statements. For complete task syntax, see SQL Management.
FROM 'tmq+ws://USER:PASSWORD@localhost:6041/dataout_demo?query=SELECT%20ts,%20tbname,%20current,%20voltage%20FROM%20dataout_demo.meters&auto.offset.reset=earliest'
TO 'postgres://USER:PASSWORD@localhost:5432/rdb_out?schema=public&table=meters_out&primary_key=ts,tbname'
Data publishing parameters for the target DSN:
| Parameter | Description | Default |
|---|---|---|
table | Target table name, required | — |
primary_key | Primary key columns separated by commas, required | — |
schema | Schema of the target table | public |
create_table | Whether to create the table when it does not exist | true |
timestamp_type | auto or bigint | auto |
batch_size | Maximum rows per batch | 1000 |
batch_timeout | Maximum wait before a batch is written, e.g. 1000, 500ms, 2s | 1s |
write_timeout | Timeout of one batch write | 60s |
max_retry_time | Total retry time for a recoverable error, at most 240 seconds | 180s |
6. Type Mapping
When taosX creates the table, it maps result columns as follows:
| TDengine type | PostgreSQL type |
|---|---|
TIMESTAMP (ms, us) | TIMESTAMPTZ(3) / TIMESTAMPTZ(6) |
TIMESTAMP (ns) | BIGINT |
BOOL | BOOLEAN |
TINYINT | SMALLINT |
SMALLINT / INT / BIGINT | same name |
TINYINT UNSIGNED | SMALLINT |
SMALLINT UNSIGNED | INTEGER |
INT UNSIGNED | BIGINT |
BIGINT UNSIGNED | NUMERIC(20,0) |
FLOAT / DOUBLE | REAL / DOUBLE PRECISION |
VARCHAR / NCHAR | VARCHAR(n) |
VARBINARY / GEOMETRY | BYTEA |
BLOB | BYTEA |
JSON (tags only) | JSONB |
DECIMAL / DECIMAL64 | not supported |
7. Write Semantics
taosX creates a TMQ query topic from the subscription SQL, writes batches to the target table by primary key, and commits the consumer position only after each write succeeds.
- Idempotent writes: rows are written with an UPSERT on the primary key, updating existing rows and inserting new ones. Task restarts, network interruptions and repeated delivery never create duplicate rows or stop the task with a primary key conflict.
- All columns are written: every result column of a row is written, including columns whose value is
NULL. When a column isNULLin TDengine, the target column is written asNULLtoo. - Repeated keys within one batch: only the last value is kept.
- Deletes are not propagated: data deleted in TDengine is kept in the target table.
- Tag values: tag values are read as they are in TDengine at consumption time.
8. Troubleshooting
| Symptom | Next check |
|---|---|
| Target connection fails | Check the network, port, target database existence, authentication, permissions, and TLS settings from the taosX host. |
| Preview succeeds but no new rows reach the target | Confirm the original task is running and the source has new data. Then check subscription SQL, consumer progress, and task activity. Preview itself does not write to the target. |
| Rows from different child tables overwrite one another | Check that the composite key includes tbname from this example and that the target constraint matches the configuration. |
| Timestamps do not match the source | Check source precision, timestamp_type, and the target column type. Compare against the same time reference. |
| Data gaps appear after downtime | Check the source WAL retention range and warnings in task activity. Use section 10 to determine what can still be resynchronized. |
For other task, write, or runtime issues, see Troubleshoot by Symptom.
9. Stop and Resume
Stop and start the original task in the Data Out task list, keeping its subscription SQL, target table, and consumer group ID. After resuming, use section 4 to check primary keys, timestamps, and business values around the interruption. Also check task activity and write errors.
Changing the consumer group ID uses a new consumer group. Do not change it merely to resume a task. If historical data needs resynchronization, first check the retained source range as described in section 10, then decide whether to use a new group. Restarting cannot recover source data that has been deleted or expired.
10. Limitations
- One subscription SQL and one target table per task. Changing the SQL, the table, the primary key, or the timestamp storage requires a new task.
- A target table can be written by only one task and should have no other writers.
- The target table must have a primary key or unique constraint on exactly the configured primary key.
- DECIMAL output is not supported. Convert it in the subscription SQL, for example with
CAST(col AS VARCHAR(64)), which keeps the exact decimal text. - Data subject to a task stop or backlog longer than the WAL retention period of the source is skipped. taosX reports a warning in the task activity when it detects this; change the consumer group ID and restart with
earliestand TSDB Data enabled to resynchronize historical data still retained at the source. This cannot recover data deleted or expired at the source. - Topic columns are fixed at creation, so columns added to the source table later are not synchronized.
11. Performance and Observability
Task metrics include messages read, rows written, batches written, retries, write errors, elapsed time of the last write, WAL data gaps, and the consumer progress per vgroup.
Write throughput mainly depends on the target database and the row width, and can be improved by tuning the batch size and batch timeout.