首页 > 解决方案 > 如何将 knex.js 连接到 PostgresSQL 数据库?

问题描述

嗨,我正在尝试使用 Knex.js 将数据库连接到 server.js 我尝试将用户添加为 postgresql,并且我还尝试将主机添加为 localhost,但这没有用,我总是得到

下面是我列出所有数据库的时间! 在此处输入图像描述

加载资源失败:服务器响应状态为 400(错误请求)

以下是我尝试注册时的错误快照!

下面是我的 register.js,它应该有助于重新注册到数据库!

在此处输入图像描述

    const handleRegister = (req, res, db, bcrypt) => {
      const { email, name, password } = req.body;
      if (!email || !name || !password) {
        return res.status(400).json('incorrect form submission');
      }
      const hash = bcrypt.hashSync(password);
        db.transaction(trx => {
          trx.insert({
            hash: hash,
            email: email
          })
          .into('login')
          .returning('email')
          .then(loginEmail => {
            return trx('users')
              .returning('*')
              .insert({
                email: loginEmail[0],
                name: name,
                joined: new Date()
              })
              .then(user => {
                res.json(user[0]);
              })
          })
          .then(trx.commit)
          .catch(trx.rollback)
        })
        .catch(err => res.status(400).json('unable to register'))
    }

module.exports = {
  handleRegister: handleRegister
};

下面是我的 server.js 文件!

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const knex = require('knex');

const register = require('./controllers/register');
const signin = require('./controllers/signin');
const profile = require('./controllers/profile');
const image = require('./controllers/image');

const db = knex({
  client: 'pg',
  connection: {
    host : 'localhost',
    user : 'postgres',
    database : 'smartbrain1'
  }
});

const app = express();

app.use(cors())
app.use(bodyParser.json());

app.get('/', (req, res)=> { res.send(db.users) })
app.post('/signin', signin.handleSignin(db, bcrypt))
app.post('/register', (req, res) => { register.handleRegister(req, res, db, bcrypt) })
app.get('/profile/:id', (req, res) => { profile.handleProfileGet(req, res, db)})
app.put('/image', (req, res) => { image.handleImage(req, res, db)})
app.post('/imageurl', (req, res) => { image.handleApiCall(req, res)})

app.listen(3000, ()=> {
  console.log('app is running on port 3000');
}) 

这是我在终端的 postgreSQL 中创建的数据库作为快照!

在此处输入图像描述

标签: postgresqlknex.js

解决方案


您应该首先尝试编写连接 pg 并运行查询的独立节点应用程序。然后,当您知道连接数据库按预期工作时,您可以开始与应用程序的其他部分集成。现在这个问题有太多不相关的信息。

首先尝试在不使用 UNIX 套接字但使用 TCP 的情况下从 shell 连接 SQL 服务器:

psql postgres://postgres@localhost/smartbrain1

如果失败,则可能意味着您的数据库已配置为不允许任何外部 TCP 连接。

要允许从 localhost 访问 postgres,这应该在 pg_hba.conf 中通过设置

host    all             all             127.0.0.1/32            trust

此外,您可能需要为您的 postgres 用户添加密码并尝试启用密码连接:

psql postgres://postgres:<password>@localhost/smartbrain1

从命令行连接时,您可以在 knex 配置中尝试这样的操作:

const db = knex({
  client: 'pg',
  connection: 'postgres://postgres:<password>@localhost/smartbrain1'
});

在这里可以找到更多用于调试的信息Knex:Error Pool2 - error: password authentication failed for user和可能在数十个其他通用 postgres 数据库连接问题中。


推荐阅读