PostgreSQL wire: Node.js (node-postgres)
The only change
Section titled “The only change”# host postgres.internal warehouse.internal# port 5432 5432 (PostgreSQL wire)# user / password / database PostgreSQL values MachWarehouse valuesnpm installDATABASE_URL=postgresql://app:...@warehouse.internal:5432/demo npm startLibrary: pg 8.23.1.
Source
Section titled “Source”The program as it is kept and tested in the Mach Database Warehouse repository; highlighted lines read the connection setting.
// Aggregate over the PostgreSQL wire with node-postgres. Plain pg code.import pg from 'pg';
const client = new pg.Client({ connectionString: process.env.DATABASE_URL });await client.connect();const { rows } = await client.query( 'SELECT country, count(*) AS events, sum(revenue) AS revenue FROM events GROUP BY country ORDER BY revenue DESC',);for (const r of rows) console.log(`${r.country}\t${r.events}\t${r.revenue}`);await client.end();{ "name": "machwarehouse-example-postgres-node", "private": true, "type": "module", "scripts": { "start": "node index.mjs" }, "dependencies": { "pg": "8.23.1" }, "engines": { "node": ">=20" }}