Здравствуйте!
Нужно присоединиться к базе PostgreSQL через NodeJS.
Файл index.js:
require('dotenv').config()
const express = require('express')
const sequelize = require('./db')
const models = require('./models/models')
const PORT = process.env.PORT || 5000
const app = express()
const start = async () => {
try {
await sequelize.authenticate()
await sequelize.sync()
app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
} catch (e) {
console.log(e)
}
}
start()
файл db.js:
const {Sequelize} = require('sequelize')
module.exports = new Sequelize(
process.env.DB_NAME, // название БД
process.env.DB_USER, // пользователь
process.env.DB_PASSWORD, // пароль
{
dialect: 'postgres',
host: process.env.DB_HOST,
host: process.env.DB_PORT
}
)
файл .env:
PORT=5000
DB_NAME=online_store
DB_USER=postgres
DB_PASSWORD=root
DB_HOST=localhost
DB_PORT=5432
PostgreSQL установлена на компьютере, но вылезает в консоли ошибка:
HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND 5432
at Client._connectionCallback (D:\OpenServer\domains\testnodereact.local\server\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:133:2
4)
at Client._handleErrorWhileConnecting (D:\OpenServer\domains\testnodereact.local\server\node_modules\pg\lib\client.js:305:19)
at Client._handleErrorEvent (D:\OpenServer\domains\testnodereact.local\server\node_modules\pg\lib\client.js:315:19)
at Connection.emit (events.js:400:28)
at Socket.reportStreamError (D:\OpenServer\domains\testnodereact.local\server\node_modules\pg\lib\connection.js:52:12)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
parent: Error: getaddrinfo ENOTFOUND 5432
parent: Error: getaddrinfo ENOTFOUND 5432
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: '5432'
},
original: Error: getaddrinfo ENOTFOUND 5432
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: '5432'
}
}