nodeJs - GradedJestRisk/db-training GitHub Wiki
You'll find a no-brainer below.
Much more here
[https://stackabuse.com/using-postgresql-with-nodejs-and-node-postgres/ A starter]
Create test data
CREATE database testdb;
CONNECT testdb;
CREATE TABLE people (name TEXT);
INSERT INTO people (name) VALUES ('calvin');
INSERT INTO people (name) VALUES ('hobbes');
COMMIT;
SELECT * FROM people;
Add dependency
npm install pg
Create connection
touch index.js
const { Client } = require('pg');
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'testdb',
password: '1234abcd',
port: 5432,
});
client.connect();
const query = `
SELECT *
FROM people
`;
client.query(query, (err, res) => {
if (err) {
console.error(err);
return;
}
for (let row of res.rows) {
console.log(row);
}
client.end();
});
Then launch
node index.js