Skip to content
Advertisement

How do I properly insert multiple rows into PG with node-postgres?

A single row can be inserted like this:

JavaScript

This approach automatically comments out any special characters.

How do i insert multiple rows at once?

I need to implement this:

JavaScript

I can just use js string operators to compile such rows manually, but then i need to add special characters escape somehow.

Advertisement

Answer

Following this article: Performance Boost from pg-promise library, and its suggested approach:

JavaScript

An example of using it, exactly as in your case:

JavaScript

And it will work with an array of objects as well:

JavaScript

UPDATE-1

For a high-performance approach via a single INSERT query see Multi-row insert with pg-promise.

UPDATE-2

The information here is quite old now, see the latest syntax for Custom Type Formatting. What used to be _rawDBType is now rawType, and formatDBType was renamed into toPostgres.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement