forked from vefforritun/vef2-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.select.js
40 lines (32 loc) · 1011 Bytes
/
01.select.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import pg from 'pg';
/*
// Aðrar leiðir til að tengjast:
// Reynir að lesa úr env breytum:
// `PGUSER`, `PGHOST`, `PGPASSWORD`, `PGDATABASE` og `PGPORT`
const pool = new pg.Pool();
// Skilgreinum hverja og eina breytu
const pool = new pg.Pool(
{ user: '', host: '', password: '', database: '', port: 1234 }
);
*/
// Notum tengistreng sem geymir allar breytur í streng, handhægt þegar
// við förum að setja upp á Heroku
const connectionString = 'postgres://vef2-2021:123@localhost/vef2-2021';
const pool = new pg.Pool({ connectionString });
pool.on('error', (err) => {
console.error('Unexpected error on idle client', err);
process.exit(-1);
});
async function main() {
const client = await pool.connect();
try {
const result = await client.query('SELECT * FROM people');
console.log('rows :>> ', result.rows);
} catch (e) {
console.error('Error selecting', e);
} finally {
client.release();
}
await pool.end();
}
main().catch((e) => { console.error(e); });