I'm trying to connect my Node project to Postgres. My server is running in the project. In Postgres, when I execute a data retrieval command, it also return me the correct data. But when he wants to retrieve data in the get method in the reposutory file in the project. He doesn't succeed and i have this erorr:Connection terminated unexpectedly
when i run the project i have in my console these messeges:
Database user: postgres Database host: xxx Database name: Dependencies_Planner Database password: xxx Database port: 5432 Server is running on port 3000
but ehwn i put this url in postman:http://localhost:3000/api/requestor-names i got this erorr in postman:Internal Server Error and status :500. and these erorrs in the console:
controller Entering getAllProductManagerNames method Error executing query in getAllProductManagerNames: Error: Connection terminated unexpectedly at C:\Users\USER\Desktop\DP\Dependencies-Planner-PRD\node_modules\pg-pool\index.js:45:11 at processTicksAndRejections (node:internal/process/task_queues:95:5) Error fetching requestor names: Error: Connection terminated unexpectedly at C:\Users\USER\Desktop\DP\Dependencies-Planner-PRD\node_modules\pg-pool\index.js:45:11 at processTicksAndRejections (node:internal/process/task_queues:95:5)
I put here all the files in my project that are related to the method. this is my .env file:
PGUSER="postgres"
PGHOST="xxx"
PGDATABASE="Dependencies_Planner"
PGPASSWORD="xxx"
PGPORT=5432
EXPRESS_PORT=3000
PGPASSWORD="***" # credentials removed
this is my db file:
import { Pool } from 'pg';
import dotenv from 'dotenv';
dotenv.config();
console.log('Database user:', process.env.PGUSER);
console.log('Database host:', process.env.PGHOST);
console.log('Database name:', process.env.PGDATABASE);
console.log('Database password:', process.env.PGPASSWORD);
console.log('Database port:', process.env.PGPORT);
const pool = new Pool({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: Number(process.env.PGPORT),
});
export default pool;
this is my repository file:
import pool from '../config/db';
export default class ProductManagerRepo {
static async getAllProductManagerNames(): Promise<{ name: string }[]> {
try {
console.log('Entering getAllProductManagerNames method');
const result = await pool.query('SELECT name FROM productmanager');
console.log('Query executed successfully, result:', result.rows);
return result.rows;
} catch (err) {
console.error('Error executing query in getAllProductManagerNames:', err);
throw err;
}
}
}
this is my controller file:
import { Request, Response } from 'express';
import ProductManagerRepo from '../repositories/productManagerRepo';
export const getRequestorNames = async (req: Request, res: Response): Promise<void> => {
console.log('controller')
try {
const productManagerNames = await ProductManagerRepo.getAllProductManagerNames();
res.json(productManagerNames);
} catch (error) {
console.error('Error fetching requestor names:', error);
res.status(500).send('Internal Server Error');
}
};
this is my model file :
export interface ProductManager{
email:string,
name:string,
groupId:number
};
this is my app file:
import express from 'express';
import dotenv from 'dotenv';
import ProductmanagerRoutes from './src/routes/productManagerRoutes'
dotenv.config();
const app = express();
const port = process.env.EXPRESS_PORT || 3000;
app.use(express.json());
app.use('/api', ProductmanagerRoutes);
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
const dbc = await pool.connect(); const result = await dbc.query('SELECT name FROM productmanager');
. The connection pool just manages (efficiently) the connections to the DB which are the actual handles to use for querying..query()
method for one-off queries that just acquires and releases a client (a connection) under the hood