-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
88 lines (63 loc) · 2.85 KB
/
database.js
File metadata and controls
88 lines (63 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { Pool, Client } = require('pg');
const connectionString = 'postgresql://postgres:a2saas177july@localhost:5432/postgres';
// Creating Client
const client = new Client({
connectionString: connectionString
});
////////////////////////////////////////// PSQL Query//////////////////////////////
// Function to perform queries
const performQueries = async () => {
try {
await client.connect();
console.log("Connected to the database.");
// Select query
const selectResult = await client.query('SELECT * FROM public.student');
console.log("Select query result:", selectResult.rows);
// Another select query
const anotherSelectResult = await client.query('SELECT * FROM student');
console.log("Another select query result:", anotherSelectResult.rows);
// Insertion query
// Make sure the column names in the insertion query match exactly with the column names in the table
const insertQuery = "INSERT INTO student (name, email) VALUES ('RJJ', 'rj@gmail.com')";
const insertResult = await client.query(insertQuery);
console.log("Insertion query result:", insertResult);
} catch (err) {
console.error("Error executing query:", err);
} finally {
await client.end();
console.log("Client disconnected.");
}
};
performQueries();
// // Step 1: Make sure your postgres database is on
// // Install pg npm i pg
// const { Pool, Client } = require('pg');
// const connectionString = 'postgresql://postgres:a2saas177july@localhost:5432/postgres';
// // Creating Client
// const client = new Client({
// connectionString: connectionString
// });
// ////////////////////////////////////////// PSQL Query//////////////////////////////
// // Function to perform queries
// const performQueries = async () => {
// try {
// await client.connect();
// console.log("Connected to the database.");
// // Select query
// const selectResult = await client.query('SELECT * FROM public.student');
// console.log("Select query result:", selectResult.rows);
// // Another select query
// const anotherSelectResult = await client.query('SELECT * FROM student');
// console.log("Another select query result:", anotherSelectResult.rows);
// // Insertion query
// const insertQuery = "INSERT INTO student (name, Email, ID) VALUES ('RJ', 'rj@gmail.com', 7)";
// const insertResult = await client.query(insertQuery);
// console.log("Insertion query result:", insertResult);
// } catch (err) {
// console.error("Error executing query:", err);
// } finally {
// await client.end();
// console.log("Client disconnected.");
// }
// };
// performQueries();