我有一个场景,我需要插入多个记录.我有一个像id(它是来自其他表的fk),key(char),value(char)的表结构.需要保存的输入将是上述数据的数组.示例:我有一些数组对象,如:
lst = [];
obj = {};
obj.id= 123;
obj.key = 'somekey';
obj.value = '1234';
lst.push(obj);
obj = {};
obj.id= 123;
obj.key = 'somekey1';
obj.value = '12345';
lst.push(obj);Run Code Online (Sandbox Code Playgroud)
在MS SQL中,我会创建TVP并通过它.我不知道如何在postgres中实现.所以我现在要做的是使用pg-promise库在postgres sql的单个查询中保存列表中的所有项目.我无法从文档中找到任何文档/理解.任何帮助赞赏.谢谢.
我正在尝试将列"isGroup"的值更改为值"public".
我创建了一个迁移:
Post.connection.execute("update Posts set isgroup='public'")
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
PGError: ERROR: column "isgroup" of relation "posts" does not exist
Run Code Online (Sandbox Code Playgroud)
遗憾的是,我在与connection.execute迁移同时运行了创建迁移的列.但是,"isGroup"列确实存在于Heroku上,因此该列未显示为出现是奇怪的.
有什么建议?
我对包含类型列的表有这个简单的查询bigint.
但是当我查询它时,pg-promise将此列的值作为字符串返回.我在文档中找不到相关信息.这是标准行为吗?
var ids = [180, 120];
db.any('SELECT id_brand, brand from catalog_brand WHERE id_brand in ($1:csv)', [ids])
.then((data) => {
// return results
});
Run Code Online (Sandbox Code Playgroud)
data 采用以下形式,id为string而不是int:
[{id_brand: "180", brand: "Ford"}, {id_brand: "120", brand: "Nike"}]
Run Code Online (Sandbox Code Playgroud)
有没有什么可以指示pg-promise返回实际类型?
可以像这样插入一行:
client.query("insert into tableName (name, email) values ($1, $2) ", ['john', 'john@gmail.com'], callBack)
Run Code Online (Sandbox Code Playgroud)
此方法会自动注释掉任何特殊字符.
如何一次插入多行?
我需要实现这个:
"insert into tableName (name, email) values ('john', 'john@gmail.com'), ('jane', 'jane@gmail.com')"
Run Code Online (Sandbox Code Playgroud)
我可以使用js字符串运算符手动编译这些行,但后来我需要以某种方式添加特殊字符转义.
我正在使用Express和Node-pg将Excel文件导入Postgres数据库
目前,我正在遍历excel行并为每一行执行一个插入操作,但是我觉得这不是正确的方法:
workbook.xlsx.readFile(excel_file).then(function () {
// get the first worksheet
var worksheet = workbook.getWorksheet(1);
// Loop through all rows
worksheet.eachRow(function (row, rowNumber) {
// Commit to DB only from line 2 and up. We want to exclude headers from excel file
if (rowNumber > 1) {
// Loop through all values and build array to pass to DB function
row.eachCell(function (cell, colNumber) {
arrSQLParams.push(cell.value)
})
// Add the user id from session to the array
arrSQLParams.push(user);
// Insert into DB …Run Code Online (Sandbox Code Playgroud) 正如我已经在 Stackoverflow 上找到的那样,可以通过执行以下操作来更新一个查询中的多行
update test as t set
column_a = c.column_a,
column_c = c.column_c
from (values
('123', 1, '---'),
('345', 2, '+++')
) as c(column_b, column_a, column_c)
where c.column_b = t.column_b;
Run Code Online (Sandbox Code Playgroud)
特别感谢@Roman Pekar 的明确答复。
现在我正在尝试将这种更新方式与查询 NodeJS 中的 postgreSQL 数据库合并。
这是我的代码片段:
var requestData = [
{id: 1, value: 1234}
{id: 2, value: 5678}
{id: 3, value: 91011}
]
client.connect(function (err) {
if (err) throw err;
client.query(buildStatement(requestData), function (err, result) {
if (err) throw err;
res.json(result.rows);
client.end(function (err) {
if …Run Code Online (Sandbox Code Playgroud) 我编写了一个 Node.js 应用程序,它将大量记录写入 PostgreSQL 9.6 数据库。不幸的是,感觉很慢。为了能够进行测试,我创建了一个简短但完整的程序来重现该场景:
'use strict';
const async = require('async'),
pg = require('pg'),
uuid = require('uuidv4');
const pool = new pg.Pool({
protocol: 'pg',
user: 'golo',
host: 'localhost',
port: 5432,
database: 'golo'
});
const records = [];
for (let i = 0; i < 10000; i++) {
records.push({ id: uuid(), revision: i, data: { foo: 'bar', bar: 'baz' }, flag: true });
}
pool.connect((err, database, close) => {
if (err) {
/* eslint-disable no-console */
return console.log(err); …Run Code Online (Sandbox Code Playgroud) 我正在使用Node js和Postgresql,并试图在连接实现中最有效率.
我看到pg-promise建立在node-postgres之上,而node-postgres使用pg-pool来管理池.
我还读到"一次超过100个客户端是一件非常糟糕的事情"(node-postgres).
我正在使用pg-promise并想知道:
是否有可能在PostgreSQL中执行多值upsert?我知道存在多值插入,如果违反了密钥则执行更新的"ON CONFLICT"关键字......但是是否可以将两者结合在一起?像这样......
INSERT INTO table1(col1, col2) VALUES (1, 'foo'), (2,'bar'), (3,'baz')
ON CONFLICT ON CONSTRAINT theConstraint DO
UPDATE SET (col2) = ('foo'), ('bar'), ('baz')
Run Code Online (Sandbox Code Playgroud)
我用谷歌搜索了这个问题并且找不到任何关于它的信息.
我有一个使用pg-promise的应用程序,我正在进行批处理.它可以工作,但它的速度非常慢(每隔5秒左右就像50行一样......).我想如果我可以取消批处理,而是正确构建这个多值upsert查询,它可以提高性能.
编辑:嗯......我只是自己尝试过,不,它不起作用.除非我做错了.所以现在我想我的问题已经改变了,实现这样的事情的好方法是什么?
有没有办法从带有$参数的查询中获取可执行查询。实际上它很奇怪,但是我想在数据库中存储可执行查询。没有参数的完整查询($ 1,$ 2,$ 3)
我正在使用node-postgres
pg.connect(conString, function(err, client, done) {
console.log('Executing Insert query');
client.query('insert into tablename(column1,column2,column3) values($1,$2,$3)',["data1","data2","data3"], function(err, result) {
done();
console.log('finished executing Insert query');
});
});
Run Code Online (Sandbox Code Playgroud)
这就是我所需要的
insert into tablename(column1,column2,column3) values("data1","data2","data3")
Run Code Online (Sandbox Code Playgroud) 我试图根据其他表中选定的数据插入数百/千个数据,但我发现错误“客户端太多”,这是错误
我使用 pgp (pg Promise) lib,这是我的代码片段
function call(){
for (let index = 0; index < 5; index++) {
getPendingData().then((result) => {
db.tx((t) => {
let counter = 0;
const queries = result.map((data) => {
counter++;
return db.none(`insert into test_data (id, isdeleted, parentid) values ('${uuidv1()}', 0, '${uuidv1()}x-${uuidv1()}' ) `);
});
return t.batch(queries);
});
});
}
}
let getPendingData = async () => {
return db.task('getPendingData', async (t) => {
return await t.any('select * from other_table');
});
}
(call())
Run Code Online (Sandbox Code Playgroud)
我设置 max pg …
postgresql ×10
node.js ×8
pg-promise ×5
javascript ×2
express ×1
heroku ×1
migration ×1
performance ×1
pgpool ×1
sql ×1
sql-insert ×1