node-postgres
node-postgres copied to clipboard
Inserting Bulk Data unsing pg
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?
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
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())