{"action":"created","issue":{"url":"https://api.github.com/repos/brianc/node-postgres/issues/880","labels_url":"https://api.github.com/repos/brianc/node-postgres/issues/880/labels{/name}","comments_url":"https://api.github.com/repos/brianc/node-postgres/issues/880/comments","events_url":"https://api.github.com/repos/brianc/node-postgres/issues/880/events","html_url":"https://github.com/brianc/node-postgres/issues/880","id":113117580,"number":880,"title":"Pass in an array of data [bulk insert] using an insert statement","user":{"login":"skilbjo","id":1145927,"avatar_url":"https://avatars.githubusercontent.com/u/1145927?v=3","gravatar_id":"","url":"https://api.github.com/users/skilbjo","html_url":"https://github.com/skilbjo","followers_url":"https://api.github.com/users/skilbjo/followers","following_url":"https://api.github.com/users/skilbjo/following{/other_user}","gists_url":"https://api.github.com/users/skilbjo/gists{/gist_id}","starred_url":"https://api.github.com/users/skilbjo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skilbjo/subscriptions","organizations_url":"https://api.github.com/users/skilbjo/orgs","repos_url":"https://api.github.com/users/skilbjo/repos","events_url":"https://api.github.com/users/skilbjo/events{/privacy}","received_events_url":"https://api.github.com/users/skilbjo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-10-23T23:40:59Z","updated_at":"2015-10-27T21:18:42Z","closed_at":"2015-10-27T21:18:42Z","body":"Hi, \r\n\r\nHow can I pass in an array of 1,000 elements and have `pg` do a bulk insert into the database?\r\n\r\nI am trying to solve this problem brought up [here](https://github.com/felixge/node-mysql/issues/1235), but using `postgres` instead\r\n\r\n````\r\nvar sql = 'insert into '+table+\r\n\t' (Month, Merchant_Id, Merchant_Descriptor, Network, Qualification_Code, Transaction_Type, '+ \r\n\t' Issuer_Type, Card_Type, Txn_Count, Txn_Amount, ' +\r\n\t'Interchange) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)';\r\n\r\npsql.connect();\r\nreduceArray(large_array, function(small_array){\r\n\tdb(small_array);\r\n});\r\n\r\nvar reduceArray = function(arr, cb){\r\n\tvar size = 1000,\r\n\t small_array = []\r\n\t ;\r\n\r\n\tfor (var i=0; i<arr.length; i+=size){\r\n\t\tvar chunk = arr.slice(i,i+size);\r\n\t\tsmall_array.push(chunk);\r\n\t}\r\n\tcb(small_array);\r\n};\r\n````\r\n\r\ndb insert:\r\n````\r\nvar db = function(small_array){\r\n\tasync.times(small_array.length, function(n, next){\r\n\t\tvar data = small_array[n]; \r\n\t\t\tpsql.query(sql, [data], function(err,result){\r\n\t\t\t\tnext(err, result);\r\n\t\t\t});\r\n\t}, function(err){\r\n\t\tconsole.log(err);\r\n\t});\r\n};\r\n````"},"comment":{"url":"https://api.github.com/repos/brianc/node-postgres/issues/comments/151647004","html_url":"https://github.com/brianc/node-postgres/issues/880#issuecomment-151647004","issue_url":"https://api.github.com/repos/brianc/node-postgres/issues/880","id":151647004,"user":{"login":"skilbjo","id":1145927,"avatar_url":"https://avatars.githubusercontent.com/u/1145927?v=3","gravatar_id":"","url":"https://api.github.com/users/skilbjo","html_url":"https://github.com/skilbjo","followers_url":"https://api.github.com/users/skilbjo/followers","following_url":"https://api.github.com/users/skilbjo/following{/other_user}","gists_url":"https://api.github.com/users/skilbjo/gists{/gist_id}","starred_url":"https://api.github.com/users/skilbjo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/skilbjo/subscriptions","organizations_url":"https://api.github.com/users/skilbjo/orgs","repos_url":"https://api.github.com/users/skilbjo/repos","events_url":"https://api.github.com/users/skilbjo/events{/privacy}","received_events_url":"https://api.github.com/users/skilbjo/received_events","type":"User","site_admin":false},"created_at":"2015-10-27T21:18:42Z","updated_at":"2015-10-27T21:18:42Z","body":"Ok sounds good. Yeah, the way I did it was just using `.map` ... I thought that this might create unnecessary overhead however (single `insert` statement for each record)...\r\n\r\nWas thinking there might be savings if you could just have one `insert` statement with an array of records. An `insert` statement for each record also works though; was just trying to save fractions of a second.\r\n\r\n````\r\ndata.map(function(item,index){\r\n  psql.query(sql, item, function(err,result){\r\n    if(err) console.log(err);\r\n  });\r\n});\r\n````"}}