「node.js」PostgreSQLのテーブルのデータを取得する

2021年8月30日

1.npmをインストール
npm i pg

2.使用例

const { Client } = require('pg')
 
const pg = new Client({
    user: 'admin',
    host: '0.0.0.0',
    database: 'sample',
    password: 'pwd345',
    port: 5432,
})
 
const srtSql = {
    name: 'test',
    text: 'SELECT * FROM user_tbl WHERE uno = $1',
    values: [1],
}
 
pg.connect()
.then(() => console.log("PostgreSqlに接続完了"))
.then(() => pg.query(srtSql))
.then(result => console.log(result.rows))
.catch((err => console.log(err)))
.finally((() => pg.end()))

 

Node.js

Posted by arkgame