我試圖讓pg-promise在NodeJS中工作以允許我連接到PostgreSQL數據庫。 node和postgre都在運行Ubuntu的codeanywhere節點框中運行。無法在NodeJS中使用pg-promise查詢PostgreSQL數據庫 - 「關係不存在」
下面是相關代碼:
var pgp = require('pg-promise')();
var bot = new TelegramBot(token, { polling: true });
var db = pgp({
host: 'localhost',
port: 5432,
database: 'users',
user: 'postgres',
password: '(pass here)'
});
db.any("select * from users")
.then(function (data) {
console.log(data);
})
.catch(function (error) {
console.log(error);
});
運行node bot.js
打印以下內容:
{ [error: relation "users" does not exist]
name: 'error',
length: 96,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '984',
routine: 'parserOpenTable' }
,並打印以下到/var/log/postgresql/postgresql-9.3-main.log
:
2016-09-29 23:09:29 EDT ERROR: relation "users" does not exist at character 15
我做了什麼錯?我想這應該是與db.any(),如下面的代碼不工作:
//db.any("select * from users")
db.connect()
.then(function (data) {
console.log(data);
})
.catch(function (error) {
console.log(error);
});
輸出是一些看起來大約類似於db
對象返回,但是這不是我所需要的..
我已經看到其他提到大寫作爲一個問題的stackoverflow問題。具體來說,他們說postgresql會使你的輸入全部小寫,而不用雙引號。要解除任何這樣的相關概念:
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
這種關係肯定存在。
我缺少什麼?
見編輯;表名爲'users' – Menasheh
你列出的是'postgres'數據庫中的用戶表,而不是你的數據庫(你也命名爲'users')。因爲'postgres ='提示符,你可以告訴你在postgres數據庫中。 – Soviut
感嘆...所以它自動被稱爲postgres,因爲這就是用戶名是什麼? – Menasheh