K
K
kr_ilya2020-07-17 12:52:11
PostgreSQL
kr_ilya, 2020-07-17 12:52:11

How to insert many rows into postgresql table at once?

It is necessary to insert in one request from 10 rows.
I insert one line like this:

pg.Query('INSERT INTO items (item, source, views, date_add, last_view, short_link, category, price, sale_price) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)', [data.item, data.source, data.views, data.date_add, data.last_view, data.short_link, data.category, data.price, data.sale_price]);


Query: function (sql, values, singleItem) {
      return new Promise((resolve, reject) => {
              try {
                  pool.query(sql, values, function (err, result) {
                      if (err) {
                          reject(err);
                      } else {
                      	// console.log('Результат: ', result)
                          resolve(singleItem ? result.rows[0] : result);
                      }
                  });
              }
              catch (e) {
                  reject(e);
              }
      });
  },

And how to insert a lot?

I saw this option.
INSERT INTO person (name, age) VALUES 
  ('john doe', 25),
  ('jane doe', 20);


But how can I apply it to my code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-17
@kr_ilya

node-postgres can't do that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question