title: How to connect knex.js to PostgresSQL database?
body: <p>Hi i'am trying to connect a database to server.js with Knex.js i have tried to add user as postgresql and i tried also to add host as localhost but that didn't work an i always get </p>
  
  <p>Below is when i list all the databases!
  <a href="https://i.stack.imgur.com/hPImh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hPImh.png" alt="enter image description here"></a></p>
  
  <p>Failed to load resource: the server responded with a status of 400 (Bad Request)</p>
  
  <p>Below is a snapshot of my error when i tries to register me!</p>
  
  <p>Below is my register.js that should help with the reigistering to the database!</p>
  
  <p><a href="https://i.stack.imgur.com/zjS4Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zjS4Q.png" alt="enter image description here"></a></p>
  
  <pre><code>    const handleRegister = (req, res, db, bcrypt) =&gt; {
        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 =&gt; {
            trx.insert({
              hash: hash,
              email: email
            })
            .into('login')
            .returning('email')
            .then(loginEmail =&gt; {
              return trx('users')
                .returning('*')
                .insert({
                  email: loginEmail[0],
                  name: name,
                  joined: new Date()
                })
                .then(user =&gt; {
                  res.json(user[0]);
                })
            })
            .then(trx.commit)
            .catch(trx.rollback)
          })
          .catch(err =&gt; res.status(400).json('unable to register'))
      }
  
  module.exports = {
    handleRegister: handleRegister
  };
  </code></pre>
  
  <p>Here is my server.js file below!</p>
  
  <pre><code>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)=&gt; { res.send(db.users) })
  app.post('/signin', signin.handleSignin(db, bcrypt))
  app.post('/register', (req, res) =&gt; { register.handleRegister(req, res, db, bcrypt) })
  app.get('/profile/:id', (req, res) =&gt; { profile.handleProfileGet(req, res, db)})
  app.put('/image', (req, res) =&gt; { image.handleImage(req, res, db)})
  app.post('/imageurl', (req, res) =&gt; { image.handleApiCall(req, res)})
  
  app.listen(3000, ()=&gt; {
    console.log('app is running on port 3000');
  }) 
  </code></pre>
  
  <p>And here is my databases that i have created in postgreSQL in the terminal as a snapshot!</p>
  
  <p><a href="https://i.stack.imgur.com/hQBb5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hQBb5.png" alt="enter image description here"></a></p>