Node.js服务器连接pgAdmin管理的PostgreSQL数据库失败及PORT环境变量未正常解析问题求助
Let's break down your two problems and fix them step by step:
The issue here is super straightforward: you're using single quotes (') instead of backticks (`) for your console log message. Template literals (which let you inject variables like ${PORT}) only work with backticks.
Change this line in index.js:
app.listen(PORT,()=>console.log('Server started on port ${PORT}'))
To this:
app.listen(PORT,()=>console.log(`Server started on port ${PORT}`))
Now when you start the server, it'll correctly show Server started on port 7000 instead of the literal ${PORT}.
There are a few common culprits here—let's go through them one by one:
2.1 Remove Quotes from Your DB Password in .env
Your .env file has DB_PASSWORD='Ondj8_oP1sw' with single quotes. The dotenv package will interpret those quotes as part of the actual password, which means Sequelize is sending 'Ondj8_oP1sw' (with quotes) instead of just Ondj8_oP1sw to PostgreSQL. That's definitely going to cause a password mismatch.
Update your .env line to:
DB_PASSWORD=Ondj8_oP1sw
2.2 Verify PostgreSQL Server & Credentials Match
- First, make sure your PostgreSQL server is actually running. In pgAdmin, check if your server instance has a green icon next to it (if it's red, right-click and select "Start Server").
- Double-check that the values in
.envexactly match what's set in pgAdmin:DB_NAME: Confirm the database exists in pgAdmin (you're usingpostgreswhich is the default, so this should be fine unless you deleted it).DB_USER: Ensure thepostgresuser has permission to access thepostgresdatabase (by default, it does, but you can verify in pgAdmin's "Login/Group Roles" section).DB_PORT: Make sure PostgreSQL is listening on port 5432. You can check this in pgAdmin by right-clicking your server > Properties > Connection > Port.
2.3 Add Debug Logs to Sequelize
To get more insight into what's going wrong with the connection, enable logging in your Sequelize config. Update db.js like this:
const {Sequelize} = require('sequelize') // Log the env vars to confirm they're loading correctly console.log("Loaded DB Config:", { name: process.env.DB_NAME, user: process.env.DB_USER, password: process.env.DB_PASSWORD, host: process.env.DB_HOST, port: process.env.DB_PORT }) module.exports = new Sequelize( process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { dialect:'postgres', host: process.env.DB_HOST, port: process.env.DB_PORT, logging: console.log // This will print detailed connection logs } )
When you start the server, you'll see exactly what Sequelize is trying to do—this can help spot issues like incorrect env vars being loaded.
2.4 Test Connection with psql (Command Line)
To rule out PostgreSQL itself being the problem, try connecting directly using the psql command line tool (it comes with PostgreSQL):
psql -h localhost -p 5432 -U postgres -d postgres
When prompted, enter your password Ondj8_oP1sw. If this works, the database is set up correctly and the issue is in your code. If it fails, you'll get a specific error message that points to the problem (like "password authentication failed" or "could not connect to server").
内容的提问来源于stack exchange,提问作者qrttttt

