Knex实例无法连接到PostgreSQL DB:未处理的拒绝错误:角色“ 19016”不存在

时间:2019-04-13 17:47:25

标签: node.js knex.js postgresql-11

我能够与KNEX在“ knex-pracice”(我的postgreql db)之间建立连接,但是当我尝试对我的knex实例进行查询时,出现了错误:“未处理的连接错误:角色” 19016“不存在”。 19016是我的WINDOWS 10系统用户帐户的名称。并且我试图弄清楚为什么使用“ 19016”而不是指定的用户“ dunder-mifflin”。我对postgres和knex都是陌生的,因此,如果我的描述有点令人困惑,或者似乎我完全误解了我的问题,请原谅我。

此外,我试图通过在数据库上创建一个名为“ 19016”的用户来尝试“破解”我的问题,并尝试以这种方式进行连接,但这只是给了我另一个错误:“未处理的连接错误:数据库“ 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"
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将connection string更改为:

postgres://user:pass@localhost:5432/dbname