pg js code example

Example 1: postgres node

//connect to postgres database in node

const Pool = require('pg').Pool;

const pool = new Pool({
    user: '',
    host: 'localhost',
    database:  '',
    password: '',
    port:5432,
  })

//query example

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: postgresql nodejs

//connect to postgres database in node

const Pool = require('pg').Pool;

const pool = new Pool({
    user: '',
    host: 'localhost',
    database:  '',
    password: '',
    port:5432,
  })

//query example

app.get('/users',async(req,res)=>{
  
  try{
     let resp=await pool.query('SELECT * FROM users');
  }catch(err){
     res.status(200).send(resp.rows);
  }
})