我使用PostgreSQL 9.4在節點7.2.1上使用pg-promise。我使用下面的代碼連接到PostgreSQL服務器:與pg-promise和postgresql的關係不存在錯誤?
// pg-promise connection elements.
// Database initialization options.
const pg_options = {
// Initialization options.
};
// Create an instance of the pg-promise library.
const g_pg_promise = require('pg-promise')(pg_options);
// Connection string for a local connection to the 'summarize' database.
const g_pg_connection =
{
host: 'localhost',
port: 5432,
database: 'db_stats',
user: 'db_user',
password: '{password here}'
}
// Connect to the database.
const g_pg_database = g_pg_promise(g_pg_connection);
// Expose the database object globally. The database object should only
// be instantiated once and then shared.
module.exports = {
g_pg_promise: g_pg_promise,
g_pg_database: g_pg_database
};
我知道連接參數是有效的,因爲我在使用其他非Node.js的應用程序完全相同的值,以連接到同一個PostgreSQL服務器。我也知道db_stats是一個有效的數據庫名稱。我已經與其他非Node.JS應用程序和pgAdmin 3.x一起使用該數據庫很長一段時間。
然而,當我嘗試使用PG-承諾我碰到下面的錯誤連接:
error: relation "db_stats" does not exist
我沒看到下面的SO發佈:
Unable to query PostgreSQL database in NodeJS using pg-promise - "relation does not exist"
但是這個職位沒有因爲我相信pg-promise的作者Vitaly告訴海報他沒有表叫用戶,wh它在我看來海報試圖訪問數據庫調用用戶他肯定有一個名稱的數據庫。
在任何情況下,我肯定有一個名爲db_stats所以我不知道爲什麼我收到此錯誤數據庫。我該如何解決這個問題?
錯誤是*不是*正在討論* *數據庫*名爲''db_stats「',而是一個*關係*(無論是*表*還是*視圖*)。也就是說,'db_stats'最可能出現在'SELECT'查詢的'FROM'子句中。 – joanolo
@joanolo謝謝,就是這樣。我非常關注連接聲明,所以我沒有想到這一點。如果您想提高評論的回覆率,我想「接受」它。 –