connect postgres with node js code example
Example 1: postgresql nodejs
const Pool = require('pg').Pool;
const pool = new Pool({
user: '',
host: 'localhost',
database: '',
password: '',
port:5432,
})
app.get('/users',async(req,res)=>{
try{
let resp=await pool.query('SELECT * FROM users');
}catch(err){
res.status(200).send(resp.rows);
}
})
Example 2: node js postgresql query
client.query('SELECT NOW() as now', (err, res) => {
if (err) {
console.log(err.stack)
} else {
console.log(res.rows[0])
}
})
client
.query('SELECT NOW() as now')
.then(res => console.log(res.rows[0]))
.catch(e => console.error(e.stack))