1

I'm trying to connect to a postgres database with Sequelize. When I try to create a row in the database, I get the following error message. I'm not sure why the port is listing 3306 when the database url I'm trying to connect to is port 5432. I tried setting the port and host name in the config file, but I still get the same error.

{
    "name": "SequelizeConnectionRefusedError",
    "message": "connect ECONNREFUSED 127.0.0.1:3306",
    "parent": {
        "code": "ECONNREFUSED",
        "errno": "ECONNREFUSED",
        "syscall": "connect",
        "address": "127.0.0.1",
        "port": 3306,
        "fatal": true
    },
    "original": {
        "code": "ECONNREFUSED",
        "errno": "ECONNREFUSED",
        "syscall": "connect",
        "address": "127.0.0.1",
        "port": 3306,
        "fatal": true
    }
}

I'm running my app in my server.js file:

const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const fs = require('fs')

const PORT = process.env.PORT || 3000


// INSTANTIATE EXPRESS APP
const app = express()

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json({
  type: function() {
    return true
  }
}))

// SET VIEW ENGINE
app.use(express.static(path.join(__dirname, 'public')))

require('./src/server/routes')(app)

app.get('*', function(request, response) {
  response.sendFile(path.resolve(__dirname, 'public', 'index.html'))
})

// SET FOLDER TO SERVER STATIC ASSETS
app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT,DELETE');
  res.setHeader(
    "Access-Control-Allow-Headers",
    "Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, username")
  //and remove cacheing so we get the most recent comments
  res.setHeader('Cache-Control', 'no-cache');
  next();
});

// SERVER APP
app.listen(PORT, () => {
  console.log('Server running on localhost:' + PORT)
})

my config file is set up to use an online database

{
  "development": {
    "url": "postgres://[email protected]:5432/znjpptuy",
    "dialect": "postgres",
    "PORT": 5432,
    "HOST": "127.0.0.1"
  }
}

1 Answer 1

1

After closer inspection, realized that the error had to do with the url I was passing to the new instance of sequelize, in this case, lack of url.

sequelize = new Sequelize(process.env[config.url]);

process.env kept coming back as undefined so I ended up passing the actual url to the function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.