簡體   English   中英

無法使用 pg-promise 在 NodeJS 中查詢 PostgreSQL 數據庫 - “關系不存在”

[英]Unable to query PostgreSQL database in NodeJS using pg-promise - "relation does not exist"

我試圖讓 pg-promise 在 NodeJS 中工作,以允許我連接到我的 PostgreSQL 數據庫。 node 和 postgre 都在運行 Ubuntu 的 codeanywhere 節點框中運行。

這是相關的代碼:

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表。 您需要先創建表,然后才能從中進行選擇。 請記住,表名區分大小寫,因此如果您創建了Users表,您的查詢將不起作用。

CREATE TABLE Users (...);
SELECT * FROM users; -- this will cause an error

您應該嘗試在命令行上使用psql -d users (在這種情況下, users是您的數據庫的名稱,不要與您的同名表混淆)連接到您的數據庫。 從那里您可以編寫查詢以獨立於您的應用程序代碼來測試它們。 如果您在連接到users數據庫時遇到問題,您始終可以使用 psql 連接到默認數據庫並鍵入\\l\\list以列出 postgres 知道的所有數據庫。

請注意,Postgres 本身有一個名為postgres的內置數據庫,該數據庫有自己的users表來管理對其他數據庫的訪問控制。 您可以通過查看命令提示符來判斷您在 psql 中連接到哪個數據庫; 如果它說postgres=那么你在 postgres 數據庫中,而不是你自己的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM