Apologies if this is a bad question but I've been troubleshooting for a while now and don't understand where the issue lies.
I have a db.js file that is meant to login and connect to my local postgres database. It pulls the login information from a .env file. When I run debugger on the db.js file I am able to connect and the login information shows in the console as loading correctly. I have an app.js file that is calling the connectDb function which is exported from my db.js file. When I run the debugger or try to run the app through the command line I get the error below:
Error connecting to the database: error: role "your-username" does not exist"
To be clear I did not edit the username out, that is the exact error message.
Below is the entirety of app.js where I'm trying to call the connectDb function from db.js.
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const playerRoutes = require('./routes/playerRoutes');
const { connectDb } = require('./config/db.js');
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// Routes
app.use('/api', playerRoutes);
// Connect to DB and start server
//connectDb().then(() => {
// app.listen(PORT, () => {
// console.log(`Server running on port ${PORT}`);
// });
//});
//console.log(process.env);
console.log('APP.js - Connecting with the following config:', {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT || 5432,
});
const startServer = async () => {
try {
await connectDb(); // Wait for DB connection before starting the server
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
} catch (error) {
console.error('Failed to start the server due to DB connection error:', error);
process.exit(1); // Exit the application in case of DB connection failure
}
};
startServer(); // Start the server after DB connection is established
The console.log('App.js - Connecting .....' displays the correct login information. And just in case it's helpful below is the db.js file:
require('dotenv').config();
console.log('DB_USER:', process.env.DB_USER);
console.log('DB_PASSWORD:', process.env.DB_PASSWORD);
console.log('DB_HOST:', process.env.DB_HOST);
console.log('DB_NAME:', process.env.DB_NAME);
const { Pool } = require('pg');
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
console.log('Connecting with user: ', process.env.DB_USER);
const connectDb = async () => {
try {
await pool.connect();
console.log('Connected to the database');
} catch (error) {
console.error('Error connecting to the database:', error);
process.exit(1); // Exit the application in case of DB connection failure
}
};
module.exports = { connectDb };
It feels like there's some timing issue going on between the two and the connectDb function is using default values but I've tried to log the function both before and during the call and both show the correct login information. I don't see anywhere in my .env file that indicates a "your-username" should exist. I've even tried hardcoding in the values into the db.js file and the issue still occurs. To summarize - in the startServer function in app.js I'm calling the connectDb function which is exported from db.js. The connection works when I debug on db.js but not when I debug on app.js.
Can anyone offer insight into my issue? Thanks in advance!
EDIT: Below is from the postgres log file.
2025-02-03 16:08:42.140 CST [7140] LOG: disconnection: session time: 0:00:00.188 user=NbaUser database=NBAProject host=::1 port=62224
2025-02-03 16:09:22.678 CST [13796] LOG: connection received: host=::1 port=62252
2025-02-03 16:09:22.679 CST [18976] LOG: connection received: host=::1 port=62254
2025-02-03 16:09:22.679 CST [13796] LOG: connection authenticated: user="your-username" method=trust (D:/Program Files/PostgreSQL/17/data/pg_hba.conf:117)
2025-02-03 16:09:22.679 CST [13796] LOG: connection authorized: user=your-username database=nba_stats
2025-02-03 16:09:22.679 CST [13796] FATAL: role "your-username" does not exist
2025-02-03 16:09:22.680 CST [22404] LOG: connection received: host=::1 port=62253
2025-02-03 16:09:22.680 CST [18976] LOG: connection authenticated: user="NbaUser" method=trust (D:/Program Files/PostgreSQL/17/data/pg_hba.conf:117)
2025-02-03 16:09:22.680 CST [18976] LOG: connection authorized: user=NbaUser database=NBAProject
2025-02-03 16:09:22.681 CST [22404] LOG: connection authenticated: user="NbaUser" method=trust (D:/Program Files/PostgreSQL/17/data/pg_hba.conf:117)
2025-02-03 16:09:22.681 CST [22404] LOG: connection authorized: user=NbaUser database=NBAProject
2025-02-03 16:09:22.753 CST [22404] LOG: could not receive data from client: An existing connection was forcibly closed by the remote host.
In pg_hba.conf
, I had all the connection settings set to trust
. When I changed them to require MD5 login method and then set a windows environment variable for PGUSER=myuser
, I was able to connect.