knex迁移:psql正常运行时,最新连接无法连接

时间:2018-06-26 02:24:50

标签: javascript postgresql knex.js postgresql-9.5

我需要帮助弄清楚为什么我无法在本地计算机上运行knex迁移  使用。似乎knex在连接到postgres数据库时遇到了一些麻烦。在终端中运行knex migration:latest会给我这个错误:

⇒  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)

我在本地Macbook上使用了postgres。这是我的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,
  },
}

运行psql可以正常工作:

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

c_dev=#

我正在knexfile.js所在的目录中运行命令。谢谢!

1 个答案:

答案 0 :(得分:2)

您的knexfile语法无效。您尚未在此处指定connection属性。

它应该像这样:

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"
    }
  }
}