使用JavaScript连接PostgreSQL数据库时出现'role "your-username" does not exist'错误的排查求助
使用JavaScript连接PostgreSQL数据库时出现'role "your-username" does not exist'错误的排查求助
抱歉,可能这是个有点基础的问题,但我已经排查好一阵子了,还是搞不懂问题出在哪。
我有个db.js文件,用来登录并连接本地的PostgreSQL数据库,它从.env文件里读取登录信息。单独调试db.js的时候,我能成功连接,控制台里也能正确显示加载的登录信息。但当我在app.js里调用db.js导出的connectDb函数时,不管是调试还是直接用命令行运行,都会报错:
Error connecting to the database: error: role "your-username" does not exist
这里要说明下,我并没有把用户名打码,这个就是完整的错误信息。
下面是我用来调用connectDb函数的app.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
控制台里console.log('APP.js - Connecting .....')这行输出的是正确的登录信息。
为了方便排查,我也把db.js的完整代码贴出来:
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 };
我感觉可能是两个文件之间有什么时序问题,导致connectDb函数用了默认值,但不管是调用前后打印信息,显示的都是正确的登录信息。我的.env文件里完全没有"your-username"这个内容,我甚至试过把db.js里的连接信息硬编码进去,结果还是报同样的错。总结下就是:单独调试db.js能正常连接,但在app.js里调用connectDb就会出错。
有没有人能帮我分析下问题出在哪?提前谢谢大家了!
另外,我还查看了PostgreSQL的日志文件,内容如下:
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.
备注:内容来源于stack exchange,提问作者Dolesy
相关产品推荐
相关产品推荐

