Knex instance failing to connect to PostgreSQL DB: Unhandled rejection Error: role "19016" doesn't exist
Knex instance failing to connect to PostgreSQL DB: Unhandled rejection Error: role "19016" doesn't exist
我能够在 "knex-pracice"(我的 postgreql 数据库)与 KNEX 之间建立连接,但是当我尝试使用我的 knex 实例进行查询时,出现错误:"Unhandled connection error: role " 19016“不存在”。 19016 是我的 WINDOWS 10 系统用户帐户的名称。我想弄清楚为什么它使用“19016”而不是指定的用户 "dunder-mifflin"。我是 postgres 和 knex 的新手,所以如果我的描述有点混乱或者我似乎完全误解了我的问题,请原谅我。
此外,我试图通过在数据库上创建一个名为“19016”的用户并尝试以这种方式连接来"hack"我的问题,但它只是给了我另一个错误:"Unhandled connection error: database "19016”不存在”。完全不知道如何让它使用用户名 "dunder-mifflin" 连接到 postgresql
但是,我可以通过我的 powershell 连接并查询数据库,没有任何问题....
.env
NODE_ENV=development
PORT=8000
DB_URL="postgresql://dunder-mifflin@localhost/knex-practice"
practice.js
//adds .env file for environment variable access
require('dotenv').config()
const knex = require('knex')
// database connection --> this connction comes from the .env file
const knexInstance = knex({
client: 'pg',
// database connection established --> environment variable comes from .env
// file
connection: process.env.DB_URL
})
console.log('connection successful');
// SQL query
knexInstance
.from('amazong_products')
.select('*')
.then(result => {
console.log(result)
});
package.json
{
"name": "knex-practice",
"version": "1.0.0",
"description": "knex-practice",
"main": "index.js",
"scripts": {
"test": "mocha --require test/setup.js",
"dev": "nodemon src/server.js",
"start": "node src/practice.js",
"predeploy": "npm audit",
"deploy": "git push heroku master"
},
"repository": {
"type": "git",
"url": "git+https://github.com/quonn-bernard/Express-Boilerplate.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/quonn-bernard/Express-Boilerplate/issues"
},
"homepage": "https://github.com/quonn-bernard/Express-Boilerplate#readme",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^7.0.0",
"express": "^4.16.4",
"helmet": "^3.16.0",
"knex": "^0.16.5",
"morgan": "^1.9.1",
"pg": "^7.9.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.2",
"nodemon": "^1.18.10",
"supertest": "^4.0.2"
}
}
您需要将 connection string 更改为:
postgres://user:pass@localhost:5432/dbname
我能够在 "knex-pracice"(我的 postgreql 数据库)与 KNEX 之间建立连接,但是当我尝试使用我的 knex 实例进行查询时,出现错误:"Unhandled connection error: role " 19016“不存在”。 19016 是我的 WINDOWS 10 系统用户帐户的名称。我想弄清楚为什么它使用“19016”而不是指定的用户 "dunder-mifflin"。我是 postgres 和 knex 的新手,所以如果我的描述有点混乱或者我似乎完全误解了我的问题,请原谅我。
此外,我试图通过在数据库上创建一个名为“19016”的用户并尝试以这种方式连接来"hack"我的问题,但它只是给了我另一个错误:"Unhandled connection error: database "19016”不存在”。完全不知道如何让它使用用户名 "dunder-mifflin" 连接到 postgresql 但是,我可以通过我的 powershell 连接并查询数据库,没有任何问题....
.env
NODE_ENV=development
PORT=8000
DB_URL="postgresql://dunder-mifflin@localhost/knex-practice"
practice.js
//adds .env file for environment variable access
require('dotenv').config()
const knex = require('knex')
// database connection --> this connction comes from the .env file
const knexInstance = knex({
client: 'pg',
// database connection established --> environment variable comes from .env
// file
connection: process.env.DB_URL
})
console.log('connection successful');
// SQL query
knexInstance
.from('amazong_products')
.select('*')
.then(result => {
console.log(result)
});
package.json
{
"name": "knex-practice",
"version": "1.0.0",
"description": "knex-practice",
"main": "index.js",
"scripts": {
"test": "mocha --require test/setup.js",
"dev": "nodemon src/server.js",
"start": "node src/practice.js",
"predeploy": "npm audit",
"deploy": "git push heroku master"
},
"repository": {
"type": "git",
"url": "git+https://github.com/quonn-bernard/Express-Boilerplate.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/quonn-bernard/Express-Boilerplate/issues"
},
"homepage": "https://github.com/quonn-bernard/Express-Boilerplate#readme",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^7.0.0",
"express": "^4.16.4",
"helmet": "^3.16.0",
"knex": "^0.16.5",
"morgan": "^1.9.1",
"pg": "^7.9.0"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.0.2",
"nodemon": "^1.18.10",
"supertest": "^4.0.2"
}
}
您需要将 connection string 更改为:
postgres://user:pass@localhost:5432/dbname