最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to query PostgreSQL database in NodeJS using pg-promise - "relation does not exist" - Stac

programmeradmin3浏览0评论

I'm trying to get pg-promise working in NodeJS to allow me to connect to my PostgreSQL database. Both node and postgre are running in a codeanywhere node box running Ubuntu.

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:

 { [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:

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("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...

I've seen other stackoverflow questions mentioning capitalization as an issue. Specifically, they say that postgresql will make your input all lowercase without double quotes. 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?

I'm trying to get pg-promise working in NodeJS to allow me to connect to my PostgreSQL database. Both node and postgre are running in a codeanywhere node box running Ubuntu.

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:

 { [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:

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("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...

I've seen other stackoverflow questions mentioning capitalization as an issue. Specifically, they say that postgresql will make your input all lowercase without double quotes. 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?

Share Improve this question edited Sep 30, 2016 at 4:52 Menasheh asked Sep 30, 2016 at 4:45 MenashehMenasheh 3,7085 gold badges34 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The error is telling you that there is no users table in your database. 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.

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 mand line. 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.

Note that Postgres itself has a built in database named postgres that has its own users table to manage access control to other databases. You can tell which database you're connected to in psql by looking at the mand prompt; If it says postgres= then you're in the postgres database, not your own.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论