Tag: node postgres

AWS Lambda上的节点function无法连接到具有node-pg的数据库

我的函数在本地工作,但是,当我部署到AWS Lambda时,似乎无法连接到我的postgres数据库。 这是错误: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }, isOperational: true, code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' } 我的数据库托pipe在Azure虚拟机上,而且在本地运行时,没有任何其他应用程序或应用程序连接到它的问题。 在Lambda上运行时,可能会导致连接失败?

带有json数组的Postgresql中的参数化查询

我想调用array_prepend到一个json []使用参数化查询。 我正在使用pg-promise npm包,但是它使用正常的node-postgres适配器。 我的查询是: db.query(`update ${schema}.chats set messages = array_prepend('{"sender":"${sender}","tstamp":${lib.ustamp()},"body":$1}',messages) where chat_id = ${chat_id}` , message)); 与“$ 1”相同。 它适用于非参数化查询。 以上代码产生: {[错误:在“hiya”或附近的语法错误] 主要原因是为了避免sql注入(文档说,使用参数化查询时,他们充分逃脱)。

为什么json_populate_record()不能识别行types?

我有2个表type和name 。 我试图通过一个函数与json_populate_record()插入到这些。 收到以下错误: first argument of json_populate_record must be a row type 表type创build使用: CREATE TABLE IF NOT EXISTS "type" ( "id" TEXT NOT NULL, "value" TEXT NOT NULL, PRIMARY KEY ("id"), CHECK ("id"<>'' AND "value"<>'') ); CREATE INDEX IF NOT EXISTS "idx_type_value" ON "type" ("value"); CREATE OR REPLACE FUNCTION "type_insertone" ( IN _a JSON […]

Node-postgres:命名参数query(nodejs)

我用我的SQL查询命名我的参数时,准备实际的原因,如在PDO的PHP。 那么我可以使用node-postgres模块的命名参数吗? 现在,我在互联网上看到很多示例和文档,显示如下的查询: client.query("SELECT * FROM foo WHERE id = $1 AND color = $2", [22, 'blue']); 但这是否正确? client.query("SELECT * FROM foo WHERE id = :id AND color = :color", {id: 22, color: 'blue'}); 或这个 client.query("SELECT * FROM foo WHERE id = ? AND color = ?", [22, 'blue']); 我问这是因为编号的参数$n ,在dynamic构build查询的情况下不能帮助我。

来自node.js的postgres连接

我在我的应用程序中使用node-postgres 。 我想知道我想要遵循的最佳实践,以确保稳定的连接。 以下是我现在使用的代码, exports.getAll = function (args, callback) { helper.client = new pg.Client('tcp://postgres:system6:5432@192.168.143.11/abc_dev'); helper.client.connect(); helper.client.query('select count(1) as total_records from facilities', function(err,response){ helper.client.query('select * ,'+response.rows[0].total_records+' as total_records from facilities', function(err,response){ callback(response); helper.client.end.bind(helper.client); }); }); }; 正如你在代码中看到的,我为每个请求连接数据库,并在查询执行后断开连接。 我还有一个想法,我只能在全局连接数据库一次,并使用打开的连接执行查询。 代码看起来像 helper.client = new pg.Client('tcp://postgres:system6:5432@192.168.143.11/abc_dev'); helper.client.connect(); exports.getAll = function (args, callback) { helper.client.query('select count(1) as total_records from facilities', […]

postgres复合types在node-postgres上

说我有以下postgreSQL复合types: CREATE TYPE myType AS( id bigint, name text, ); 和一个除了那个types的存储过程: CREATE FUNCTION myFunction(mt myType){ //do some stuff } 我想使用node-postgres模块从Node-js调用这个过程。 var pg = require('pg'); var connectionString = "connection string"; pg.connect(connectionString, function(err, client, done) { client.query('SELECT myFunction($1)', [some value], function(err, result) { // do stuff done(); }); }); 我如何在JS中创build这样的types? 有没有办法将节点types传递给Postgres存储过程?

Node – 使用node-postgres设置Postgres驱动程序

我正在尝试整合node-postgres驱动程序,并学习做一个简单的CRUD操作。 在我的app.js ,我做了这样的事情: … var postgres = require('./adapters/postgres') var postClient = new postgres(conf); … postClient.connect(function (dbconn) { app.dbconn = dbconn; app.conf = conf; console.log("************************************************************"); console.log(new Date() + ' | CRUD Server Listening on ' + conf['web']['port']); console.log("************************************************************"); server.listen(conf['web']['port']); var Routes = require('./routes/http-routes'); new Routes(app); }); 在我的adapters/postgres.js文件中,我有以下内容: const Client = require('pg'); const postClient = new Client(conf)({ […]

pg(node-postgres)是否会自动清理数据

我正在使用node-postgres生产应用程序,我想知道是否有什么我应该关心的? 数据是否由node-postgres自动清理? 在github页面上找不到任何关于它的地方: https : //github.com/brianc/node-postgres

Node-postgresdate不准确保存

我正在使用Node.js,Postgres和node-postgres库。 当我尝试用这样的代码插入一个新的logging与当前date: client.query('INSERT INTO ideas(date) VALUES($1)', [new Date()], …); 这运行正常,没有错误。 但是,当我对数据库运行select语句并logging结果时,给出的date显示为: Wed Nov 20 2013 19:00:00 GMT-0500 (EST) 除了当插入logging的时候,这是好的,11月21日是星期四。时间是5:47,而不是7:00。 我再次运行代码,不pipe时间如何,即使下一个小时已经开始,它也会保存相同的不准确的date。 这使我相信,出于某种原因,它只存储date而不是时间或分钟。 另外,date只有一天的事实表明这个问题可能与node-postgres处理date的方式有关。 我知道这不是一个计算当前date时,将它传递到查询中的问题,因为我loggingnew Date() ,它是准确的date,分钟,小时和秒。 任何有关这个问题的帮助将不胜感激。 谢谢!

用brianc / node-postgres批量插入到Postgres中

我在nodejs中有下面的代码使用pg( https://github.com/brianc/node-postgres )我的代码为员工创build订阅是这样的。 client.query( 'INSERT INTO subscriptions (subscription_guid, employer_guid, employee_guid) values ($1,$2,$3)', [ datasetArr[0].subscription_guid, datasetArr[0].employer_guid, datasetArr[0].employee_guid ], function(err, result) { done(); if (err) { set_response(500, err, res); logger.error('error running query', err); return console.error('error running query', err); } logger.info('subscription with created'); set_response(201); }); 正如你已经注意到datasetArr是一个数组。 我想一次为多个员工创build批量订阅。 但是我不想循环访问数组。 有没有办法做到这一点与pg的框?