node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Inserting Bulk Data unsing pg

Open arater opened this issue 2 years ago • 2 comments

I am using pg.pool to connect and do operations on the database. But is there any way to insert bulk data while using pg? I can insert it while using for-loop or something like that but is there any efficient way to do that? If any compatible library you know you can suggest it?

arater avatar Mar 24 '22 16:03 arater

just create a prepared statement

const valueStr = "($1, $2), ($3, $4)"
const valueArr = ["userA", 10, "userB", 20]
pg.query(`INSERT INTO user(name, age) VALUES ${valuesStr}`, valueArr)

just create the valueStr and valueArr by some customized function

jackhts4 avatar Mar 25 '22 06:03 jackhts4

Hello, I am trying the same thing here, I want to insert multiple phone numbers from the array but it is not working as I expected, any support is apperciated.

let pnum = ['23144224', '23155323', '23148839', '231983323']
connect_db()
    .then(() => console.log("Successfully Connected"))
    .then(() => {
        pnum.foreach((num) => client.query('INSERT INTO myschema.phonelist (num) VALUES (num);'))
    })
    .then(results => console.log(results))
    .catch(e => console.log(e.detail))
    .finally(() => client.end())

elhananjair avatar Aug 31 '23 13:08 elhananjair