nodejs postgresql

  • pgsql.js
const { Client } = require('pg');
const config = {
    host: "127.0.0.1",
    database: "postgres",
    user: "postgres",
    password: "postgres",
    port: 5432, // 默认的PostgreSQL端口号
}

const pg = require("pg");
const pool = new pg.Pool({
    host: "127.0.0.1",
    database: "postgres",
    user: "postgres",
    password: "postgres",
    port: 5432,
    max: 20,
    idleTimeoutMillis: 3000
})

//新建连接的方法
function qurey_files(req, res) {
    var strSql = 
    "select name, path, suffix, status, \
    to_char(upload_date_time,'yyyy-mm-dd_HH24:MI:ss') as time \
    from public.\"Test\" \
    ORDER BY key ASC";
    let client = new Client(config);
    client.connect();
    client.query(strSql, (err, result) => {
        if (err) {
            res.status(404).send(err.message);
            console.log(err);
        }
        else {
            res.send(result.rows);
        }
        client.end();
    })
}

//使用线程池的方法
function qurey_files_pool(req, res) {
    var strSql = 
    "select name, path, suffix, status, \
    to_char(upload_date_time,'yyyy-mm-dd_HH24:MI:ss') as time \
    from public.\"Test\" \
    ORDER BY key ASC";
    pool.connect((err, client, done)=>{
        // if (err) throw err;
        client.query(strSql, (err, result)=>{
            done();
            res.send(result.rows);
        })
    })
}

module.exports = {
    qurey_files,
    qurey_files_pool
}
  • server.js
const db = require('./server/pgsql')
db.qurey_files_pool(req, res)

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 14:52:09       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 14:52:09       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 14:52:09       87 阅读
  4. Python语言-面向对象

    2024-05-09 14:52:09       96 阅读

热门阅读

  1. MY SQL 实验二:

    2024-05-09 14:52:09       27 阅读
  2. 机器学习理解梯度

    2024-05-09 14:52:09       22 阅读
  3. 如何在Python中处理异常?

    2024-05-09 14:52:09       30 阅读
  4. mysql IF语句,模糊检索

    2024-05-09 14:52:09       29 阅读
  5. c++--引用干货

    2024-05-09 14:52:09       32 阅读
  6. opencv 轮廓区域检测

    2024-05-09 14:52:09       38 阅读
  7. C++堆结构与堆排序深度解析

    2024-05-09 14:52:09       32 阅读
  8. js 关于数组排序的方法

    2024-05-09 14:52:09       32 阅读
  9. C++ 可变参数模板

    2024-05-09 14:52:09       38 阅读