Created
September 1, 2023 08:09
-
-
Save bsmth/3e6724a91de65978163ff137700fd6fd to your computer and use it in GitHub Desktop.
Adding list of phone numbers into pg database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Pool = require("pg").Pool; | |
const pool = new Pool({ | |
user: "bsmth", | |
host: "localhost", | |
database: "demo_db", | |
password: "password", | |
port: 5432, | |
}); | |
// CREATE TABLE phone_numbers (person_name TEXT, phone_number int); | |
const getRows = () => { | |
pool.query("SELECT * FROM phone_numbers", (error, results) => { | |
if (error) { | |
throw error; | |
} | |
console.log(results.rows); | |
}); | |
}; | |
let numbers = [12354, 12345, 12346, 12347, 12348, 12349, 12350]; | |
const insertRows = () => { | |
numbers.forEach((number) => { | |
pool.query( | |
"INSERT INTO phone_numbers (person_name, phone_number) VALUES ($1, $2)", | |
["some_name", number], | |
(error, results) => { | |
if (error) { | |
throw error; | |
} | |
console.log(results); | |
} | |
); | |
}); | |
}; | |
const endConnection = () => { | |
pool.end(); | |
}; | |
insertRows(); | |
getRows(); | |
endConnection(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment