[英]problem about connecting to my Postgres with node.js through knex
I am facing this problem with connecting to my Postgres with node.js
through knex
.我通过
knex
使用node.js
连接到我的 Postgres 时遇到了这个问题。 I am trying this for the first time and I ask humbly to help me solving the issue.我是第一次尝试这个,我谦虚地请求帮助我解决这个问题。 please help me.
请帮我。
My code is the following.我的代码如下。 Every time I make a request, PostgreSQL doesn't connect so nothing happens.
每次我提出请求时,PostgreSQL 都不会连接,所以什么也没有发生。
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex')
const db = knex({
client: 'pg',
connection: {
host: '127.0.0.1',
user: 'postgres',
password: '',
database: 'smart-brain'
}
});
db.select('*').from('users').then(console.log).catch(console.log);
app.use(cors());
app.post('/signin', (req, res) => {
if (req.body.email === database.users[0].email &&
req.body.password === database.users[0].password) {
res.json('success');
} else {
res.status(400).json('error logging in');
}
})
app.post('/register', (req, res) => {
const {
name,
email,
password
} = req.body;
db('users')
.returning('*')
.insert({
email: email,
name: name,
joined: new Date()
})
.then(respons => {
res.json(response);
}).
catch(err => res.status(400).json('unable to register'))
})
app.listen(3000, () => {
console.log('app is running on the port 3000');
});
and the response is these on npm
并且响应是
npm
上的这些
Error: connect ECONNREFUSED 127.0 .0 .1: 5432
at TCPConnectWrap.afterConnect[as oncomplete](net.js: 1141: 16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
If you are in Ubuntu, then go to the following folder.如果你在 Ubuntu,那么 go 到以下文件夹。
/etc/postgresql/{your_pg_version}/main
Or If you are in Windows, then go to the following folder,或者如果你在 Windows,那么 go 到以下文件夹,
C:\Program Files\PostgreSQL\{your_pg_version}\data\
Open the file pg_hba.conf
to write with SuperUser/Administrative permission,打开文件
pg_hba.conf
以使用 SuperUser/Administrative 权限进行写入,
Go to the bottom, and put trust
at the end of following lines. Go 到底部,并将
trust
放在以下行的末尾。
# Database administrative login by Unix domain socket
local all postgres trust
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
After that, restart your PostgreSQL server and try again with your code.之后,重新启动您的 PostgreSQL 服务器并使用您的代码重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.