Postgresql Knex未从knexfile读取连接字符串

Postgresql Knex未从knexfile读取连接字符串,postgresql,knex.js,Postgresql,Knex.js,我收到了这样一个knexfile: require('dotenv').config() module.exports = { client: 'pg', connection: process.env.DB_CONNECTION, pool: { min: 2, max: 10 }, migrations: { tableName: 'knex_migrations' } }; 我提供的连接字符串是: Host=localhost;Datab

我收到了这样一个knexfile:

require('dotenv').config()

module.exports = {
  client: 'pg',
  connection: process.env.DB_CONNECTION,
  pool: {
    min: 2,
    max: 10
  },
  migrations: {
    tableName: 'knex_migrations'
  }
};
我提供的连接字符串是:

Host=localhost;Database=heypay;Username=postgres;Password=1234
但是,Knex不断发出错误:

 password authentication failed for user "user"

显然,我给出的用户名不是用户。此外,我还尝试将连接字符串硬存储到
module.exports
下的连接字段中。这仍然是徒劳的。

我使用的是.NET样式的连接字符串,正确的连接字符串格式如下:

{
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
}

诀窍是,
连接
属性可以是字符串或对象。这就是为什么您能够提供一个环境变量(它是一个字符串)

原始字符串失败的原因不是Knex问题:格式稍有不同。您可以使用与第一次尝试类似的方法,但请注意密钥名称:

host=localhost port=5432 dbname=mydb connect_timeout=10
还要注意空格,而不是分号。但是根据我的经验,大多数人使用Postgres URI:

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]
因此,在您的示例中,您将使用:

module.exports = {
  client: 'pg',
  connection: 'postgresql://your_database_user@localhost/myapp_test',
  pool: {
    min: 2,
    max: 10
  },
  migrations: {
    tableName: 'knex_migrations'
  }
};

还有连接字符串的选项。但是我在文档中找不到它,只是要求您将它放在一个env var@the_ehT中