I missed call to the client.end(); Now application exits properly:
pg.connect(dbUrl, function(err, client, done) {
var i = 0, count = 0;
for (i = 0; i < 1000; i++) {
client.query(
'INSERT into post1 (title, body, created_at) VALUES($1, $2, $3) RETURNING id',
['title', 'long... body...', new Date()],
function(err, result) {
if (err) {
console.log(err);
} else {
console.log('row inserted with id: ' + result.rows[0].id);
}
count++;
console.log('count = ' + count);
if (count == 1000) {
console.log('Client will end now!!!');
client.end();
}
});
}
});