简体   繁体   English

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

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

I'm trying to get pg-promise working in NodeJS to allow me to connect to my PostgreSQL database.我试图让 pg-promise 在 NodeJS 中工作,以允许我连接到我的 PostgreSQL 数据库。 Both node and postgre are running in a codeanywhere node box running Ubuntu. node 和 postgre 都在运行 Ubuntu 的 codeanywhere 节点框中运行。

Here's the relevant code:这是相关的代码:

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 prints the following:运行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' } 

And prints the following to /var/log/postgresql/postgresql-9.3-main.log :并将以下内容打印到/var/log/postgresql/postgresql-9.3-main.log

2016-09-29 23:09:29 EDT ERROR:  relation "users" does not exist at character 15

What did I do wrong?我做错了什么? I guess it should be something with db.any(), as the following code does work:我想它应该与 db.any() 有关,因为以下代码确实有效:

//db.any("select * from users")
db.connect()
    .then(function (data) {
        console.log(data);
    })
    .catch(function (error) {
        console.log(error);
    });

The output is a return of something that looks approximately like the db object, but that's not what I need...输出是一个类似于db对象的东西的返回,但这不是我需要的......

I've seen other stackoverflow questions mentioning capitalization as an issue.我已经看到其他 stackoverflow 问题提到大写是一个问题。 Specifically, they say that postgresql will make your input all lowercase without double quotes.具体来说,他们说 postgresql 会让你的输入全部小写,没有双引号。 To dispell any such notion of relevance:消除任何此类相关概念:

postgres=# \dt                                                                                             
         List of relations                                                                                 
 Schema | Name  | Type  |  Owner                                                                           
--------+-------+-------+----------                                                                        
 public | users | table | postgres                                                                         
(1 row) 

The relation certainly does exist.这种关系确实存在。

What am I missing?我错过了什么?

The error is telling you that there is no users table in your database.该错误告诉您数据库中没有users表。 You need to create the table before you can select from it.您需要先创建表,然后才能从中进行选择。 Remember, the table name is case sensitive so if you created a Users table, your query won't work.请记住,表名区分大小写,因此如果您创建了Users表,您的查询将不起作用。

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

You should try connecting to your database using psql -d users ( users in this case is the name of your database, not to be confused with your table of the same name) on the command line.您应该尝试在命令行上使用psql -d users (在这种情况下, users是您的数据库的名称,不要与您的同名表混淆)连接到您的数据库。 From there you can write queries to test them independently from your application code.从那里您可以编写查询以独立于您的应用程序代码来测试它们。 In case you're having trouble connecting to your users database, you can always connect to the default database with psql and type \\l or \\list to list all databases postgres knows about.如果您在连接到users数据库时遇到问题,您始终可以使用 psql 连接到默认数据库并键入\\l\\list以列出 postgres 知道的所有数据库。

Note that Postgres itself has a built in database named postgres that has its own users table to manage access control to other databases.请注意,Postgres 本身有一个名为postgres的内置数据库,该数据库有自己的users表来管理对其他数据库的访问控制。 You can tell which database you're connected to in psql by looking at the command prompt;您可以通过查看命令提示符来判断您在 psql 中连接到哪个数据库; If it says postgres= then you're in the postgres database, not your own.如果它说postgres=那么你在 postgres 数据库中,而不是你自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM