Hi guys I’ve been trying to make a simple query to my database and for the life of me I cannot accomplish anything. I’ve tried writing a query like;
const { Pool } = require(‘pg’);
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DATABASE,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
but that just resulted in errors stating I needed to input the password as a string (which I did). Next I have been trying to use a connectionString instead of that to no avail. I have been trying with the following code which has just led me to ‘Error: read ECONNRESET’.
const { Pool } = require(‘pg’);
const connectionString = ‘postgres://admin:…some password ........@dpg-cjkcqtdk5scs73cvb7t0-a.oregon-postgres.render.com/merdbv’;
async function connectAndQuery() {
const pool = new Pool({
connectionString,
});
try {
const result = await pool.query(‘SELECT * FROM public.“Products”’);
console.log(result.rows); // Log or process the query results here
// When done, you can end the pool
await pool.end();
} catch (error) {
console.error(‘Error:’, error.message);
}
}
// Call the async function to execute the database query
connectAndQuery();
I have tried so many things, and googled so many different errors, I literally am at a complete wit’s end. Any input on this would be very much appreciated. Code is in javascript, done in visual studio.
Edit: Wanted to mention I’ve successfully connected on pgadmin, but I’m trying to get it to work on visual studio so that I can make read/write queries from my website.
EDIT 2:My second snippet of code attempting the first style I tried;
const { Pool } = require(‘pg’);
require(‘dotenv’).config();
const pool = new Pool({
connect details, etc
});
async function fetchData() {
try {
const result = await pool.query(‘SELECT * FROM public.“Products”’);
// ‘result.rows’ contains the retrieved data
return result.rows;
} catch (error) {
throw error;
}
}
// Call the fetchData function to get the data
fetchData()
.then((data) => {
console.log(data); // Use the data as needed
})
.catch((error) => {
console.error(‘Error:’, error.message);
});