Search code examples
node.jspostgresqlknex.js

Error when attempting knex seed:run after successful knex migrate:latest for remote database


I'm running the following error when attempting to run knex seed:run against my remote postgres database (not localhost): Knex:Error Pool2 - Error: connect ECONNREFUSED 127.0.0.1:5432.

I am able to run knex migrate:latest successfully and can see that the tables are created on my postgres server, but when I try to seed I get that error. I've run the same migrations/seed file against my local configuration and it has worked without a problem, but when I attempt to seed my heroku postgres instance, it throws this error (I'm not running my local pg service when I'm seeding the new db, which is likely why it's throwing an error).

Any thoughts on why it is attempting to connect to localhost instead of the specified db? Sample of my file provided below:

var User = require("./models/User");
var Project = require("./models/Project");

exports.seed = function(knex, Promise) {
  console.log(knex.client.config.connection); //This returns the correct db info.
  return knex('user').del()
    .then(function() {
      return knex('project').del()
    }).then(function() {
      return new User({id: 1, firstName: "James", lastName: "Lee", phone: "123-456-2000", email: "[email protected]"}).save(null, {method: "insert"});
    }).then(function() {
      return new Project({id: 1, name: "Test"}).save(null, {method: "insert"});
    })
};

Solution

  • This seems to have occurred due to how I was setting up the migrations / seeds. The configurations were actually pulling from two different places, one which had the correct SSL settings in place, the other without (seed file). Adding the correct settings in both places seemed to resolve the issue.