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