标签: node-postgres

node-postgres是否支持多个结果集

我有一个PostgresQL函数返回多个结果集.我可以在.net中提取这些结果集而没有问题(所以我知道我的函数正常工作),但我在使用node-postgres时遇到了麻烦.

结果对象返回一个包含7个项目的数组,这些项目与返回的数据集数量相匹配.

在Node中,7行中的每一行都只包含一个字符串<unnamed portal 1>.

connection.query("BEGIN");
connection.query({text: "SELECT getoperationaldatasetmodel($1)", values : [clientid]}, function(err, results) {


  if (err) {
    connection.query("COMMIT");
    self.pool.release(connection);
    callback(err);
  }
  else {
    var opsDataset = null;
    var rows = results.rows;
    // this returns 7 rows but the rows do not contain data but rather the name of the dataset.
  }
Run Code Online (Sandbox Code Playgroud)

那么:node-postgres是否支持多个结果集,如果是,那么有关如何提取的任何建议?

编辑:这是我用node-postgres的代码,如果其他人需要将来使用它.

// must wrap in a transaction otherwise won't be able to see the multiple sets.
connection.query("BEGIN");
connection.query({text: "SELECT myfunction($1)", values : [clientid]}, function(err, results) …
Run Code Online (Sandbox Code Playgroud)

postgresql node.js node-postgres

5
推荐指数
1
解决办法
2215
查看次数

Node-postgres将新记录插入我的数据库不会返回新条目的数据

这是执行查询的路径:

userRouter.route("/new")
    .post(function (req, res) {
        var user = req.body;
        pg.connect(connectionString, function (error, client, done) {
            var queryString = "INSERT INTO Users (id, first_name, last_name) VALUES (" + "'" + [user.id, user.first_name, user.last_name].join("','") + "'" + ")";
            console.log(queryString);
            client.query(queryString, function (error, result) {
                console.log(result.rows);
                done();
            });
        });
    });
Run Code Online (Sandbox Code Playgroud)

问题是我试图从第二个控制台引用的"结果"值基本上是空白的:

{
  command: 'INSERT',
  rowCount: 1,
  oid: 0,
  rows: [],
  fields: [],
  _parsers: [],
  RowCtor: null,
  rowAsArray: false,
  _getTypeParser: [Function: bound ]
 }
Run Code Online (Sandbox Code Playgroud)

不应该result.rows包含一个数组,其中包含一个表示我刚刚在数据库中创建的行的对象?

javascript postgresql express node-postgres

4
推荐指数
2
解决办法
4520
查看次数

将POINT插入postgres数据库

我需要在postgres数据库中插入/更新点列类型.

我正在使用 node-postgres

使用POSTGRES管理面板生成的脚本将更新查询显示为

UPDATE public.places SET id=?, user_id=?, business_name=?, alternate_name=?, primary_category=?, categories=?, description=?, address=?, city=?, state=?, country=?, zip=?, point WHERE <condition>;

我如何从纬度和经度中获得点数?

我已经看过几个使用POSTGIS的答案,但无法使其正常工作.

在POSTGRES(https://www.postgresql.org/docs/9.2/static/xfunc-sql.html)的文档中提到我们可以使用point '(2,1)',但这不适用于pg查询.

我现在拥有的:

var config = {
  user: 'postgres',
  database: 'PGDATABASE',
  password: 'PGPASSWORD!',
  host: 'localhost',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
};
Run Code Online (Sandbox Code Playgroud)

更新部分:

app.post('/updatePlaces', function(req, res, next) {
    console.log("Update");
    console.log(req.body.places);
    pool.query('UPDATE places SET address = $1, alternate_name = $2, business_name = $3, categories = $4, city = $5, country = $6, description …
Run Code Online (Sandbox Code Playgroud)

postgresql postgis node-postgres

4
推荐指数
3
解决办法
1万
查看次数

使用pg-promise的连接池

我正在使用Node js和Postgresql,并试图在连接实现中最有效率.
我看到pg-promise建立在node-postgres之上,而node-postgres使用pg-pool来管理池.
我还读到"一次超过100个客户端是一件非常糟糕的事情"(node-postgres).

我正在使用pg-promise并想知道:

  1. 对于非常大的数据负载,推荐的poolSize是什么?
  2. 如果poolSize = 100并且应用程序同时获得101请求(甚至更多)会发生什么?Postgres是否处理订单并使101请求等待它可以运行它?

postgresql node.js pgpool node-postgres pg-promise

4
推荐指数
2
解决办法
3163
查看次数

如何检查表是否存在?

在 db/index.js 中:

const { Pool } = require('pg'); //node-postgres
const pool = new Pool;

module.exports = {
    query: (text, params, callback) => {
        return pool.query(text,params,callback);
    }
};
Run Code Online (Sandbox Code Playgroud)

在我的 main.js 中:

const db = require('./db/index');

    db.query('SELECT count(*) from pg_class where relname ="session" and relkind="r"', (err, res) => {
        if (err) {
          //TODO: What to do if there is an error?
        }
        console.log(res);
    });
Run Code Online (Sandbox Code Playgroud)

输出是:

不明确的

如何检查名称为“session”的表是否存在?我怎么知道我的查询是否有效?

编辑:这不是上述问题的重复,因为我的问题与 Node.js javascript 代码有关。不是 SQL!我只想知道要在 res 对象中查找什么,因为它似乎未定义...

postgresql node.js node-postgres

4
推荐指数
1
解决办法
8773
查看次数

使用 node-postgress 连接到 PostgreSQL@localhost 时遇到困难(错误:28000)

我目前正在 rPi3B+ (aarch64) 上运行 openSuse,并且在运行 NodeJS 连接脚本时遇到了困难。

我完成了 PostgreSQL 的标准安装(v10 是此版本的 openSuse 上提供的),然后创建了一个新角色

CREATE ROLE new_role LOGIN PASSWORD 'passwd';
Run Code Online (Sandbox Code Playgroud)

然后是一个数据库

CREATE DATABASE new_db OWNER new_role;
Run Code Online (Sandbox Code Playgroud)

&\l返回\du预期的输出表明角色和数据库均已使用正确的所有者成功创建。

因此,我然后快速创建了一个节点项目目录并从文档复制了测试脚本: https: //node-postgres.com/features/connecting

const { Pool, Client } = require('pg')
const connectionString = 'postgresql://new_role:passwd@localhost:5432/new_db'

const pool = new Pool({
  connectionString: connectionString,
})

pool.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

const client = new Client({
  connectionString: connectionString,
})
client.connect()

client.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  client.end() …
Run Code Online (Sandbox Code Playgroud)

postgresql node-postgres

4
推荐指数
1
解决办法
1万
查看次数

如何在 node-postgres 中捕获 client.connect()?

try/catch 不适用于 client.connect():

const { Pool, Client } = require('pg')
try {
  const client = new Client(config)
  client.connect()
} catch (er) {
  console.log('error')
}
Run Code Online (Sandbox Code Playgroud)

错误:

UnhandledPromiseRejectionWarning: error: password authentication failed for user '...'
Run Code Online (Sandbox Code Playgroud)

如何将异步更改connect()为 try/catch 同步?

postgresql error-handling node.js node-postgres

4
推荐指数
1
解决办法
514
查看次数

node-postgres不会插入数据,但也不会抛出错误

我正在使用node.js 的node-postgres模块,我在插入数据时遇到问题.

功能:

function addItems(listId, listItems, handle) {
    if (!listItems) {
        handle(listId);
        return;
    }
    var client = dbConnector.getClient(),
        important,
        dateArray,
        dateString,
        i,
        prepStatement;
    client.connect();
    for (i in listItems) {
        console.log(listItems[i]);
        dateArray = listItems[i].itemdate.split('-');
        dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2];
        if (listItems[i].important) {
            important = 'true';
        } else {
            important = 'false';
        }
        prepStatement = {
            name: 'insert task',
            text: 'INSERT INTO listitem (todolist_id, name, deadline, description, important, done, created) VALUES ($1, $2, $3, …
Run Code Online (Sandbox Code Playgroud)

node.js node-postgres

3
推荐指数
1
解决办法
2504
查看次数

使用 json 格式的 array_agg 检索一对多关系行 - postgresql

我有两个表,分别为:1. 采访 2. 问题

每个面试表还可以多出一个问题。

我想以这样的方式检索数据:在一行中,面试详细信息以及所有问题及其详细信息在单列中呈现,并以数组格式与该特定面试相关联。

我已经尝试了 array_agg()、json_object_build() 的所有方法,但似乎无法使其正常工作。

模式: 表模式图像

SQL 查询我现在有:

SELECT
         i.id as interview_id, i.board, i.time_taken, i.notes, i.interview_date, 
         u.id as user_id, u.first_name, u.last_name, u.state, u.district, u.optional, 
         j.name as job, 
         json_build_object('question', q.question, 'answer', q.answer, 'member', q."member", 'order', q."order") as questions
         FROM interview i
         LEFT JOIN question q ON q.interview_id = i.id 
         INNER JOIN users u ON i.user_id = u.id 
         INNER JOIN user_jobs uj ON uj.user_id = u.id 
         INNER JOIN job j ON uj.job_id = j.id
         GROUP BY u.id, …
Run Code Online (Sandbox Code Playgroud)

sql postgresql node-postgres

3
推荐指数
1
解决办法
2189
查看次数

PG (Node-Postgres) 池在 Connect 上挂起......但仅限于 Gatsby 内部?

注意:这主要是关于 Node-PostgreSQL 模块的问题pg。它包含来自 Gatsby 和 Postgraphile 的详细信息,但我不需要这三个方面的专业知识,只需pg.

我有一个数据库,可以与使用 PostGraphile 的 Express 服务器配合使用。node我还可以通过命令行访问它......

const { Pool } = require("pg");
const pool = new Pool({ connectionString: myDbUrl });
pool.connect().then(() => console.log('connected'));
// logs 'connected' immediately
Run Code Online (Sandbox Code Playgroud)

完全相同的数据库之前也通过gatsby-source-pg插件与 Gatsby/PostGraphile 配合得很好...但最近我更换了开发机器,当我尝试构建或运行开发服务器时,Gatsby 挂在“源和转换节点”步骤上。当我调试它时,它挂在对 的调用上pool.connect()

所以我实际上有两个使用 PostGraphile 的代码库,两者都具有相同的配置,一个可以工作,另一个则不能。更奇怪的是,如果我编辑 Gatsby 插件的源代码node_modules,使其使用完全相同的代码(我可以在命令行成功运行)......它仍然挂起。

我唯一能想到的是其他一些 Gatsby 插件正在耗尽所有连接并且没有释放它们,但据我所知(例如通过 grep-ing through node_modules)没有其他插件甚至使用pg.

所以我真的有两个问题:

A)谁能帮我理解为什么connect会挂起?如果你能帮助我理解为什么它会使用已知良好的配置并且在 Gatsby 内部(在某些环境因素发生变化后)这样做,那就加分了?

B) 谁能帮我解决这个问题吗?如果可能是某种“以前的代码忘记释放连接”问题,有什么方法可以测试吗?如果我能以某种方式记录,new …

node.js node-postgres gatsby postgraphile

3
推荐指数
1
解决办法
1313
查看次数