我刚刚使用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) 我正在尝试使用node-postgres模块将一个小文件存储到postgres数据库中.我知道我应该使用bytea数据类型来执行此操作.我遇到的问题是当我做一些事情时:
fs.readFile path, (err, data) ->
client.query 'UPDATE file_table SET file = $1 WHERE key = $2', [data, key], (e, result) ->
....
Run Code Online (Sandbox Code Playgroud)
db中文件列的内容为:\ x,不存储任何内容.如果我将数据缓冲区更改为十六进制,即data.toString('hex'),则会存储该文件,但是当我读回文件时,所有格式都会丢失.
使用node-postgres模块将文件存储到postgres的正确方法是什么?
我[error: relation "causes" does not exist]从节点应用程序收到错误.这种关系确实存在,我不确定问题是什么.
我创建了表
CREATE TABLE causes (
cause_id bigint NOT NULL default nextval('causes_cause_id_seq'::regclass),
cause_name varchar(40) NOT NULL,
goal integer,
sponsor varchar(30),
organization varchar(30),
submitter varchar(30),
address varchar(34),
balance numeric
);
Run Code Online (Sandbox Code Playgroud)
这是给出错误的查询:
client = pg.connect(connectionString, function(err, client, done){
if(err) console.log(err);
client.query('INSERT INTO causes (cause_name, goal, organization, sponsor, submitter) VALUES ($1,$2,$3,$4,$5) RETURNING *', r, function(err, result){
if(err) console.log(err);
});
});
Run Code Online (Sandbox Code Playgroud) 如果可以动态确定名称并且仍然阻止SQL注入攻击,那么如何正确提供表名?
例如:
以下作品,但我认为是不安全的:
dbclient.query("INSERT INTO " + table_name + " VALUES ($1, $2, $3)", [value_a, value_b, value_c])
我想等同于(但不起作用)的是:
dbclient.query("INSERT INTO $1 VALUES ($2, $3, $4)", [table_name, value_a, value_b, value_c])
编辑:我正在使用node-postgres:https://github.com/brianc/node-postgres.
我想使用node-postgres模块在postgres中创建一个"预备语句".我想创建它而不将其绑定到参数,因为绑定将在循环中进行.
在我阅读的文档中:
query(object config, optional function callback) : Query
If _text_ and _name_ are provided within the config, the query will result in the creation of a prepared statement.
Run Code Online (Sandbox Code Playgroud)
我试过了
client.query({"name":"mystatement", "text":"select id from mytable where id=$1"});
Run Code Online (Sandbox Code Playgroud)
但是当我尝试只传递配置对象中的文本和名称键时,我得到一个例外:
(已翻译)消息绑定0参数但预准备语句需要1
有什么我想念的吗?如何在不将其绑定到特定值的情况下创建/准备语句,以避免在循环的每个步骤中重新准备语句?
在启动Node.js应用程序时,我想对PostgreSQL数据库进行几个同步调用,以在继续控制流程之前检查一些事情。如何使用node-postgres包实现此目的?
我从postgres获得一些日期文件,格式如下:
"2000-11-30T14:00:00.000Z"
我无法在页面上的可编辑日期字段中使用此功能.就像是:
<a href="#" editable-date="employee.brthday"
onbeforesave="updateField($data, 'brthday', employee)">
{{employee.brthday || 'empty' | date:"dd/MM/yyyy" }}
</a>
Run Code Online (Sandbox Code Playgroud)
这个日期(如上所示)显示正常.但是,当我想编辑此字段时,日期将重置,我在控制台中收到此消息:
Error: [ngModel:datefmt] Expected `2000-12-05T14:00:00.000Z` to be a date http://errors.angularjs.org/1.3.0/ngModel/datefmt?p0=2000-12-05T14%3A00%3A00.000Z
at http://localhost:8000/bower_components/angular/angular.js:80:12
at Array.<anonymous> (http://localhost:8000/bower_components/angular/angular.js:19453:17)
at Object.ngModelWatch (http://localhost:8000/bower_components/angular/angular.js:20555:36)
at Scope.$digest (http://localhost:8000/bower_components/angular/angular.js:13957:40)
at Scope.$apply (http://localhost:8000/bower_components/angular/angular.js:14227:24)
at HTMLAnchorElement.<anonymous> (http://localhost:8000/bower_components/angular-xeditable/dist/js/xeditable.js:831:21)
at HTMLAnchorElement.jQuery.event.dispatch (http://localhost:8000/bower_components/jquery/dist/jquery.js:4409:9)
at HTMLAnchorElement.elemData.handle (http://localhost:8000/bower_components/jquery/dist/jquery.js:4095:28)
Run Code Online (Sandbox Code Playgroud)
如果我只是通过编辑字段来更新模型(输入新日期),将来可以编辑正常,因为日期存储如(Date obj?):
Wed Dec 06 2000 00:00:00 GMT + 1000(Якутскоевремя(зима))
如何将输入日期转换为角度格式可理解?
我还尝试用'new Date(输入日期 - 这里)替换输入日期格式,但它不起作用.可能是输入日期格式无法从字符串中解析?
总结:我需要将输入日期格式转换为Date obj或通过像Date对象这样的pg.js日期字段获取.我该怎么做呢?
尝试将以下文件与 Webpack 捆绑在一起失败
./~/pg/lib/native/index.js 模块中的错误未找到:错误:无法解析 .../node_modules/pg/lib/native @ ./~/pg/lib/ 中的模块“pg-native”本机/index.js 9:13-33
我ignore在.babelrc 中尝试了几个语句,但没有让它运行......
我要捆绑的测试文件:handler.js
const Client = require('pg').Client;
console.log("done");
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
module.exports = {
entry: './handler.js',
target: 'node',
module: {
loaders: [{
test: /\.js$/,
loaders: ['babel'],
include: __dirname,
exclude: /node_modules/,
}]
}
};
Run Code Online (Sandbox Code Playgroud)
.babelrc
{
"plugins": ["transform-runtime"],
"presets": ["es2015", "stage-1"]
}
Run Code Online (Sandbox Code Playgroud)
包.json
"dependencies": {
"postgraphql": "^2.4.0",
"babel-runtime": "6.11.6"
},
"devDependencies": {
"babel-core": "^6.13.2",
"babel-loader": "^6.2.4",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-preset-es2015": "^6.13.2",
"babel-preset-stage-0": "^6.5.0",
"babel-polyfill": "6.13.0",
"serverless-webpack": "^1.0.0-rc.3",
"webpack": "^1.13.1" …Run Code Online (Sandbox Code Playgroud) 我正在使用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) 我正在node-pg与打字稿一起使用。
我有一个来自文档的 getPool 实用程序https://node-postgres.com/features/pooling
export const getPool = (config?: PoolConfig) => {
const pool = new Pool(config);
pool.on('error', (err, client) => {
console.error('Unexpected error on idle client', err);
process.exit(-1);
});
return pool;
};
Run Code Online (Sandbox Code Playgroud)
我在异步/等待上下文中像这样使用它
const pool = getPool();
await pool.query('my sql query here...');
Run Code Online (Sandbox Code Playgroud)
当我有一个无效的 SQL 查询时,我会收到此类错误:
error: null value in column "foo" violates not-null constraint
at Parser.parseErrorMessage (node_modules/pg-protocol/src/parser.ts:357:11)
at Parser.handlePacket (node_modules/pg-protocol/src/parser.ts:186:21)
at Parser.parse (node_modules/pg-protocol/src/parser.ts:101:30)
at Socket.<anonymous> (node_modules/pg-protocol/src/index.ts:7:48)
Run Code Online (Sandbox Code Playgroud)
注意:我会理解是否是pool.on('error')s 回调窃取了我的堆栈跟踪,但错误没有以Unexpected error on idle client
请注意,在堆栈跟踪中没有任何行指向我的代码库中的文件。 …
node-postgres ×10
node.js ×7
postgresql ×7
angularjs ×1
babeljs ×1
database ×1
date ×1
express ×1
javascript ×1
pg ×1
sql ×1
typescript ×1
webpack ×1
x-editable ×1