我刚刚使用node-postgres开始使用postgres来使用node.js.我试图做的一件事就是写一个简短的js来填充我的数据库,使用一个包含大约200,000个条目的文件.
我注意到在一段时间后(少于10秒),我开始得到"错误:连接终止".我不确定这是否是我使用node-postgres的问题,或者是因为我是垃圾邮件postgres.
无论如何,这是一个显示此行为的简单代码:
var pg = require('pg');
var connectionString = "postgres://xxxx:xxxx@localhost/xxxx";
pg.connect(connectionString, function(err,client,done){
if(err) {
return console.error('could not connect to postgres', err);
}
client.query("DROP TABLE IF EXISTS testDB");
client.query("CREATE TABLE IF NOT EXISTS testDB (id int, first int, second int)");
done();
for (i = 0; i < 1000000; i++){
client.query("INSERT INTO testDB VALUES (" + i.toString() + "," + (1000000-i).toString() + "," + (-i).toString() + ")", function(err,result){
if (err) {
return console.error('Error inserting query', err);
}
done();
});
}
}); …Run Code Online (Sandbox Code Playgroud)