PostgreSQL Node for Node-RED

0.15.4 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates preparing `msg.query` and `msg.params` in a Node-RED Function node for an upsert operation into a PostgreSQL database, typically followed by the `node-red-contrib-postgresql` node.

/* 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;

view raw JSON →