What is the process for installing the PostgreSQL node module?
As always npm init -y to create a blank package.json file and then ‘node install pg’
Sitting down to write the JS to connect to the DB, what is the first step?
You need to get the Pool object from pg using the require ( ) syntax. For example:
const { Pool } = require( ‘pg’ ) // this is for node
How can you create a pool instance?
const user = '' const host = 'localhost' const database = '' const password = '' const port = ''
const pool = new Pool({
user,
host,
database,
password,
port
}) // don't forget to update the generic valuesHow is a basic query performed?
(async ( ) => {
const res = await pool.query('SELECT name FROM dogs')
for (const row of res.rows) {
console.log(row.name)
}
})()Does the ‘pg’ library support promises/callbacks
Yes it does, but async syntax is better
How are inserts performed?
const name = 'Roger'
const age = 8
try {
await pool.query('INSERT INTO dogs VALUES $1, $2', name, age)
} catch(err) {
console.error(err)
}