-
Notifications
You must be signed in to change notification settings - Fork 1
PostgreSQL node
GradedJestRisk edited this page Oct 20, 2024
·
1 revision
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
</code>
Create connection
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