Answers for "postgresql nodejs"

5

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);
  }
})
Posted by: Guest on April-11-2020
8

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);
  }
})
Posted by: Guest on April-20-2020
0

node js postgresql query

// callback
client.query('SELECT NOW() as now', (err, res) => {
  if (err) {
    console.log(err.stack)
  } else {
    console.log(res.rows[0])
  }
})
// promise
client
  .query('SELECT NOW() as now')
  .then(res => console.log(res.rows[0]))
  .catch(e => console.error(e.stack))
Posted by: Guest on May-05-2021
0

postgresql nodejs

const { Client } = require('pg')const client = new Client();(async () => {  await client.connect()  const res = await client.query('SELECT $1::text as message', ['Hello world!'])  console.log(res.rows[0].message) // Hello world!  await client.end()})()
Posted by: Guest on September-08-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language