PostgreSQL Node for Node-RED
node-red-contrib-postgresql is a Node-RED contribution that provides a node for interacting with PostgreSQL databases. It is currently at version 0.15.4 and has a relatively steady release cadence, primarily focused on bug fixes, dependency updates, and minor enhancements. Key features include robust support for parameterized SQL queries (both numeric $1/$2 and emulated named $id parameters), efficient handling of large datasets through resultset splitting and backpressure (flow control), and flexible dynamic SQL query input via `msg.query`. It provides database responses in `msg.payload`, with additional metadata in `msg.pgsql`. The node integrates directly into the Node-RED flow editor, abstracting away direct `node-postgres` client management for most use cases, though advanced users can dynamically configure client settings via `msg.pgConfig` for specific scenarios.
Common errors
-
TypeError: Cannot read properties of null (reading 'config')
cause The PostgreSQL node was deployed without being correctly linked to a valid PostgreSQL configuration node, or the configuration node was deleted or became invalid.fixEnsure that the PostgreSQL query node is connected to a properly configured and deployed PostgreSQL configuration node in your Node-RED flow. -
syntax error at or near "..."
cause The SQL query provided in the node's configuration or via `msg.query` contains invalid PostgreSQL syntax, or Mustache templating was used incorrectly (e.g., missing quotes for text values).fixCarefully review the SQL query for any syntax errors. If using Mustache templates for text values, ensure they are correctly quoted (e.g., `WHERE name = '{{{msg.name}}}'`). Prefer parameterized queries to avoid such issues and enhance security. -
Error: connect ECONNREFUSED <ip>:<port>
cause The Node-RED instance cannot establish a connection with the PostgreSQL database server. This often means the server is not running, is not accessible from the Node-RED host, or the connection details (host, port) are incorrect.fixVerify that your PostgreSQL database server is running and network accessible. Double-check the host IP address/hostname and port number in your PostgreSQL configuration node. -
Error: password authentication failed for user "..."
cause The username or password provided in the PostgreSQL connection configuration is incorrect or lacks the necessary permissions to connect to the specified database.fixConfirm that the username and password configured in the PostgreSQL connection node are correct and that the user has appropriate database access privileges.
Warnings
- gotcha Named parameters (e.g., `$id` in queries with `msg.queryParameters`) are emulated by this library and not natively supported by PostgreSQL. This emulation can be less robust than standard numeric parameters (`$1`, `$2`).
- gotcha Providing dynamic connection parameters via `msg.pgConfig` for each message bypasses the internal connection pooling mechanism of the Node-RED node. This can lead to decreased performance and resource inefficiency compared to using the persistent configuration node.
- gotcha When the 'Split results' option is enabled and 'Number of rows per message' is set to 1, the `msg.payload` output will contain a single-row object directly, rather than the typical array of objects.
- breaking Directly embedding user-supplied values into SQL query templates using Mustache (e.g., `{{{msg.id}}}`) can introduce SQL injection vulnerabilities if inputs are not properly sanitized.
- breaking Versions prior to `0.15.3` contained a bug that could lead to `pg-cursor` exceptions, particularly when processing large result sets with the 'Split results' and backpressure features enabled.
Install
-
npm install node-red-contrib-postgresql -
yarn add node-red-contrib-postgresql -
pnpm add node-red-contrib-postgresql
Imports
- PostgreSQL Node (Conceptual)
import { PostgresqlNode } from 'node-red-contrib-postgresql';Install 'node-red-contrib-postgresql' via Node-RED palette manager.
- msg.query for Dynamic SQL
import { query } from 'node-red-contrib-postgresql';// In a preceding Function node: msg.query = 'SELECT * FROM my_table WHERE id = $1';
- msg.params for Parameterized Queries
import { params } from 'node-red-contrib-postgresql';// In a preceding Function node: msg.params = [msg.payload.id];
Quickstart
/* This is a Node-RED Function node preparing a message for the PostgreSQL node */
// Example 1: Dynamic SQL query
// msg.query = 'SELECT * FROM users WHERE status = $1';
// msg.params = ['active'];
// Example 2: Insert data with parameterized query
const userId = 'user_' + Date.now();
const username = 'testuser_' + Math.floor(Math.random() * 1000);
const email = `${username}@example.com`;
msg.query = `
INSERT INTO public.users (id, username, email, created_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (id) DO UPDATE SET
username = EXCLUDED.username, email = EXCLUDED.email;
`;
msg.params = [userId, username, email];
// Example 3: Select data by ID using named parameters (less robust, prefer numeric)
// msg.query = 'SELECT * FROM products WHERE id = $productId;';
// msg.queryParameters = { productId: 123 };
return msg;