Skip to content

Instantly share code, notes, and snippets.

@bsmth
Created September 1, 2023 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsmth/3e6724a91de65978163ff137700fd6fd to your computer and use it in GitHub Desktop.
Save bsmth/3e6724a91de65978163ff137700fd6fd to your computer and use it in GitHub Desktop.
Adding list of phone numbers into pg database
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