Pond Premtoon
Pond Premtoon

Reputation: 93

knex migration:latest cannot connect while psql works fine

I need help figuring out why I can't run knex migration on my local machine using. It seems that knex has some trouble connecting to the postgres database. Running knex migration:latest in the terminal gives me this error:

⇒  knex migrate:latest
Using environment: development
Error: Unable to acquire a connection
at Client_PG.acquireConnection (/Users/moldot/prjx-albert/server/node_modules/knex/lib/client.js:332:40)
at Runner.ensureConnection (/Users/moldot/prjx-albert/server/node_modules/knex/lib/runner.js:233:24)
at Runner.run (/Users/moldot/prjx-albert/server/node_modules/knex/lib/runner.js:47:42)
at SchemaBuilder.Target.then (/Users/moldot/prjx-albert/server/node_modules/knex/lib/interface.js:39:43)
at Migrator._ensureTable (/Users/moldot/prjx-albert/server/node_modules/knex/lib/migrate/index.js:256:66)
at Migrator._listCompleted (/Users/moldot/prjx-albert/server/node_modules/knex/lib/migrate/index.js:405:17)

I'm using postgres on my local Macbook. This is my knexfile:

var PostgressConnectionStringParser = require('pg-connection-string');

module.exports = {
  heroku: {
    client: 'pg',
    connection: process.env.DATABASE_URL,
    migrations: {
      directory: "migrations",
      tableName: "migrations",
    },
  },
  development: {
    client: "pg",
    host: "localhost",
    port: 5432,
    username: "moldot",
    database: "c_dev",
    migrations: {
      directory: "migrations",
      tableName: "migrations",
    },
    ssl: true,
  },
}

Running psql works fine:

⇒  psql -h localhost -p 5432 -U moldot -d c_dev
psql (10.1, server 9.5.4)
Type "help" for help.

c_dev=#

I'm running the command in the same directory that knexfile.js is in. Thank you!

Upvotes: 2

Views: 1767

Answers (1)

Mikael Lepistö
Mikael Lepistö

Reputation: 19718

Your knexfile has an invalid syntax. You have not given the connection attribute there.

It should look like this:

module.exports = {
  heroku: {
    client: 'pg',
    connection: process.env.DATABASE_URL,
    migrations: {
      directory: "migrations",
      tableName: "migrations"
    }
  },
  development: {
    client: "pg",
    connection: {
      host: "localhost",
      port: 5432,
      username: "moldot",
      database: "c_dev",
    },
    migrations: {
      directory: "migrations",
      tableName: "migrations"
    }
  }
}

Upvotes: 3

Related Questions