node.js - Postgres NodeJS 批量更新插入超时
问题描述
我正在尝试将 10k 行的 CSV 插入 Postgres。它适用于前 400 行,然后出现以下错误:
"Error: connection terminated"
我不确定如何调试它,因为错误非常普遍。这是我的代码:
const fs = require("fs");
const Pool = require("pg").Pool;
const csvFilePath='../../li.csv'
const csv=require('csvtojson')
async function run(){
const jsonArray=await csv().fromFile(csvFilePath);
let datas=[];
jsonArray.map((item)=>{
datas.push(item);
})
// create a new connection to the database
const pool = new Pool({
host: "host",
user: "host",
database: "db",
password: "host",
port: 5432
});
const query =
"INSERT INTO table_name (profile, name, tle, loc, rel, current, time) VALUES ($1, $2, $3, $4, $5, $6, $7)";
pool.connect((err, client, done) => {
if (err) throw err;
try {
datas.forEach(row => {
client.query(query, [row.profile, row.name, row.tle, row.loc, row.rel, row.current, row.time], (err, res) => {
if (err) {
console.log(err.stack);
} else {
console.log("inserted " + res.rowCount + " row:", row);
}
});
});
} finally {
done();
}
})
}
run();
解决方案
推荐阅读
- json - 将本机 Concat 字符串反应到 Json 对象
- c# - 为什么我可以从命令行运行我的 ASP.NET Core MVC 应用程序,但不能通过按 F5 从 Visual Studio 运行?
- python-3.x - 我想在一行中取两个整数并在python3中计算它们的总和
- dll - 使用多变量函数时出现 Fortran DLL 错误
- security - 如何修复 Laravel 5.2.45 版中的“URL 重写漏洞”
- r - 使用gglot2的嵌套图
- c++ - OpenGL 窗口伪影
- r - 从数据框中的列中添加和减去值
- bash - 如何提取活动域
- php - Ajax 搜索结果,点击结果下载文件