Tag: node postgres

ForEach循环内asynchronous/等待节点Postgres查询

编辑:我使用节点v8.0.0 我刚刚开始学习如何使用node-postgres访问SQL数据库,并且在访问多个数据库以便以可工作的格式收集数据时遇到了一些问题,特别是在forEach循环中执行多个查询时。 经过几次尝试,我试着asynchronous/等待,但我得到以下错误: await client.connect() ^^^^^^ SyntaxError: Unexpected identifier 当我尝试使用一个池或顺序调用.query,我会得到的东西沿线 1 [] could not connect to postgres Error: Connection terminated 这里是我的代码的缩写版本: const { Client } = require('pg'); const moment = require('moment'); const _ = require('lodash'); const turf = require('@turf/turf'); const connString = // connection string var collected = [] const CID = 300 const snaptimes […]

如何使用json插入非空列的表?

我试图用多个非空的默认列插入到food表中,命令如下: food_insertone('{"id": 1, "taste": "sweet"}'::JSON) food_insertone('{"id": 2}'::JSON) food_insertone('{"id": 3, "taste": null}'::JSON) 结果应该是这样的: INSERTED 1, 'sweet' INSERTED 2, '' ERROR (null not allowed in taste) 餐桌被定义为: CREATE TABLE "food" ( "id" INT, "taste" TEXT NOT NULL DEFAULT '', … ); CREATE OR REPLACE FUNCTION "food_insertone" (JSON) RETURNS VOID AS $$ INSERT INTO "food" SELECT * FROM […]

如何使用node-postgres插入多行?

这是我的陈述: INSERT INTO userpermissions (username, permission) VALUES ($1, $2),($3,$4) RETURNING *; 这是我的代码: db.query(stmt2,values2, (err2, result2) => { //Do other stuff depending on if there is an error or result } 其中stmt2是上面的语句,而values2是我创build的数组: var values2 = []; for(i=0;i<permissions.length;i++){ values2.push(username); values2.push(permissions[i]); } 有一些错误/致命的exception,但我不能看到它,因为错误的域被包装。

连接到postgresclosures'此套接字已closures'的错误消息

我正在向node.js 0.6.12迁移,现在在使用pg模块(版本0.6.14)时得到以下错误信息: Error: This socket is closed. at Socket._write (net.js:453:28) at Socket.write (net.js:446:15) at [object Object]._send (/home/luc/node_modules/pg/lib/connection.js:102:24) at [object Object].flush (/home/luc/node_modules/pg/lib/connection.js:192:8) at [object Object].getRows (/home/luc/node_modules/pg/lib/query.js:112:14) at [object Object].prepare (/home/luc/node_modules/pg/lib/query.js:150:8) at [object Object].submit (/home/luc/node_modules/pg/lib/query.js:97:10) at [object Object]._pulseQueryQueue (/home/luc/node_modules/pg/lib/client.js:166:24) at [object Object].query (/home/luc/node_modules/pg/lib/client.js:193:8) at /home/luc/test/routes/user.js:23:29 我的代码中显示的行是: var get_obj = client.query("SELECT id FROM users WHERE name = $1", […]

如何在用户浏览器上实现node-postgres?

所以我testing了这个特殊的例子在我的本地机器上: http://bjorngylling.com/2011-04-13/postgres-listen-notify-with-node-js.html 有效! 所以现在当我更新一个特定的表,并运行我的node.js文件(从教程) – 我得到我的terminal(mac)的即时通知! 凉! 但是,我如何实现这个客户端的浏览器? 首先,在node.js脚本中,您将注意到我必须使用我的用户名和密码连接到数据库: pgConnectionString = "postgres://username:pswd@localhost/db"; 我显然不能在.js文件中下载用户的浏览器。 另外,我甚至不知道我要在<head>包含哪些脚本。 我在任何地方都找不到如何在真实世界中使用它……我所看到的只是在命令行中可以使用的整齐的小例子。 任何build议,或在正确的方向指导将是真棒! 谢谢。

是否有可能得到的SQL语句一起由node-postgres返回的错误?

我从node-postgres (当调用它的client.query方法的时候)得到这样的错误: { [Error: syntax error at or near "as"] severity: 'ERROR', code: '42601', position: '60', file: 'scan.l', line: '1044', routine: 'scanner_yyerror' } } 如果我能看到有问题的SQL以及错误消息,那么debugging将变得更容易。 是否有可能得到它? 我意识到我可以手动实现(wrap client.query等),但是我感兴趣的是node-postgres本身是否可以被强制提供SQL。

用node-postgres查询postgres数据库

每次查询数据库时,是否需要使用pg.connect()? 在查看了githhub页面和wiki之后,这些例子在pg.connectcallback中显示了一个查询(注释来自于github示例,我没有写它们) //this initializes a connection pool //it will keep idle connections open for a (configurable) 30 seconds //and set a limit of 20 (also configurable) pg.connect(conString, function(err, client, done) { if(err) { return console.error('error fetching client from pool', err); } client.query('SELECT $1::int AS number', ['1'], function(err, result) { //call `done()` to release the client […]

Postgres:无法从NodeJS(PG)连接到md5

Windows 10上的Node v6.2; PG(node-postgres)v4.4.2 Ubuntu 14.04上的Postgres 9.5 Trusty VirtualBox 我正在使用PG(node-postgres)模块在虚拟机上连接到Ubuntu上的Postgres。 我能够使用Navicat连接没有问题,但是当我连接NodeJS时,那些相同的凭据不起作用。 postgres日志显示这个: xxxx@yyyy FATAL: password authentication failed for user "xxxx" xxxx@yyyy DETAIL: Connection matched pg_hba.conf line 96: "host all all all $ 我添加了这行到我的pg_hba.conf文件,现在它的工作,但任何人都可以解释为什么md5不工作? # I added this line: host all all all password # This line was already here: host all all all md5

为什么这个蓝鸟PG代码挂起?

我试图围绕蓝鸟诺言包裹头,并通过文档中的一些例子。 我目前的代码是基于这个例子 : var Promise = require('bluebird'); var pg = Promise.promisifyAll(require('pg')); var using = Promise.using; function getConnection(string) { var close; return pg.connectAsync(string).spread(function(client, done) { close = done; return client; }).disposer(function() { console.log('In disposer'); try { if (close) close(); } catch(e) {}; }); }; using(getConnection('/var/run/postgresql dc'), function(conn) { console.log('Got a connection'); return conn.queryAsync('SELECT 1'); }) .then(function(rows) […]

PG似乎并没有正确closures连接

我正在编写一个使用Node.js和postgresql的应用程序。 当用户validation它击中数据库来检查用户的密码,然后应该closures连接到数据库。 但是,如果用户input了错误的密码并再次尝试进行身份validation,则在服务器控制台上出现此错误: UNCAUGHT EXCEPTION… { [Error: write EPIPE] code: 'EPIPE', errno: 'EPIPE', syscall: 'write' } Error: write EPIPE 我相信它不是第一次正确closures连接。 这是应该closures连接的脚本的一部分: query.on('end', function(){ client.end(); if (pass == this.dbpass){ console.log(pass); console.log(this.dbpass); return callback (200, "OK", {}, true); } else { console.log(pass); return callback(200, "OK", {}, false); } }); 有没有另一种方法来closures我不知道的连接?