COPY, not a loop of INSERTs
Inserting a million rows one statement at a time spends most of the time on round trips and per-statement overhead. COPY streams them in one go and is typically ten to fifty times faster.
COPY events (id, kind, payload) FROM STDIN WITH (FORMAT csv);Every driver exposes it: copy_from in psycopg, COPY ... FROM STDIN through pg.query streams in node. Drop indexes before a huge load and rebuild after, and run ANALYZE when it is done.
postgresqlperformance