Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs postgresql local connection

const pgp = require('pg-promise')(/* initialization options */);

const cn = {
    host: 'localhost', // server name or IP address;
    port: 5432,
    database: 'myDatabase',
    user: 'myUser',
    password: 'myPassword'
};

// alternative:
// var cn = 'postgres://username:password@host:port/database';

const db = pgp(cn); // database instance;

// select and return a single user name from id:
db.one('SELECT name FROM users WHERE id = $1', [123])
    .then(user => {
        console.log(user.name); // print user name;
    })
    .catch(error => {
        console.log(error); // print the error;
    });

// alternative - new ES7 syntax with 'await':
// await db.one('SELECT name FROM users WHERE id = $1', [123]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js format indian price with commas 
Javascript :: react input cursor jump end 
Javascript :: Uncaught (in promise) Error: Redirected when going from "/login" to "/" via a navigation guard. 
Javascript :: javascript save data to local storage 
Javascript :: javascript constant variable 
Javascript :: remove index from array javascript 
Javascript :: a go to id js 
Javascript :: javascript map function 
Javascript :: vscode add shortcut to run in terminal 
Javascript :: password reset passport-local mongoose 
Javascript :: is javascript faster than python 
Javascript :: react-infinite-scroller 
Javascript :: ajax file upload input 
Javascript :: nodejs append to json 
Javascript :: simple node rest 
Javascript :: how to install moralis and react-moralis 
Javascript :: WebPack basic Configuration 
Javascript :: javascript objects 
Javascript :: setup react app from cpanel 
Javascript :: how to console.log variable in js 
Javascript :: Adding A forEach Function To An HTMLCollection JavaScript 
Javascript :: change inptu val 
Javascript :: javascript tostring 
Javascript :: grid in chart.js 
Javascript :: round 2 decimales js 
Javascript :: C# Convert Json File to DataTable 
Javascript :: headers with fetch 
Javascript :: javascript array methods 
Javascript :: create a pdf puppeteer js 
Javascript :: server side rendering 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =