无法使用 pg-promise 在 NodeJS 中查询 PostgreSQL 数据库 - “关系不存在”

2023-12-01

我正在尝试让 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);
    });

Running 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 or \list列出 postgres 知道的所有数据库。

请注意,Postgres 本身有一个内置数据库,名为postgres拥有自己的users表来管理对其他数据库的访问控制。您可以通过查看命令提示符来判断您在 psql 中连接到哪个数据库;如果它说postgres=那么你就在 postgres 数据库中,而不是你自己的数据库中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 pg-promise 在 NodeJS 中查询 PostgreSQL 数据库 - “关系不存在” 的相关文章

随机推荐