showing results for - "nodejs postgresql local connection"
Paulina
29 Jan 2020
1const pgp = require('pg-promise')(/* initialization options */);
2
3const cn = {
4    host: 'localhost', // server name or IP address;
5    port: 5432,
6    database: 'myDatabase',
7    user: 'myUser',
8    password: 'myPassword'
9};
10
11// alternative:
12// var cn = 'postgres://username:password@host:port/database';
13
14const db = pgp(cn); // database instance;
15
16// select and return a single user name from id:
17db.one('SELECT name FROM users WHERE id = $1', [123])
18    .then(user => {
19        console.log(user.name); // print user name;
20    })
21    .catch(error => {
22        console.log(error); // print the error;
23    });
24
25// alternative - new ES7 syntax with 'await':
26// await db.one('SELECT name FROM users WHERE id = $1', [123]);
27