Postgres报告关系不存在,但表存在

时间:2013-12-22 12:54:00

标签: postgresql express

我有一个express应用,我正在连接到Postgres数据库。这是我的代码:

var express = require('express');
var app = express();
var pg = require('pg').native;
var connectionString = process.env.DATABASE_URL || 'postgres://localhost:5432/isx';
var port = process.env.PORT || 3000;
var client;


app.use(express.bodyParser());

client = new pg.Client(connectionString);
client.connect();

app.get('/users', function(req, res) {
  'use strict';
  console.log('/users');
  var query = client.query('SELECT * FROM users');
  query.on('row', function(row, result) {
    result.addRow(row);
  });
  query.on('end', function(result) {
    console.log(result);
    res.json(result);
  });
});

我转到我的本地Postgres并查看isx数据库,以下是可用的表格。

          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | projects | table | postgres
 public | users    | table | postgres
(2 rows)

但是当我尝试点击users表时,我收到此错误Error: relation "users" does not exist

关系users存在。我已经检查过,并且我已连接到我认为已连接的Postgres实例。还有什么我可以遗漏的?

1 个答案:

答案 0 :(得分:2)

检查潜在的权限问题,例如不正确的搜索路径,或不正确的权限,或该订单的某些内容。您可以运行此查询以显示所有可用表,例如:

select relname
from pg_class c
where pg_table_is_visible(c.oid)
and relkind = 'r'
and relname not like E'pg\_%';

也可能值得研究区分大小写的相关问题。例如,也许pg库在小写标识符周围添加双引号,并且您使用CamelCase创建了表,或者按照该顺序创建了一些表。

然后检查search_path是否相关:

show search_path;

如果路径正确,则检查权限,例如使用:

select usename, nspname || '.' || relname as relation,
       case relkind when 'r' then 'TABLE' when 'v' then 'VIEW' end as relation_type,
       priv
from pg_class join pg_namespace on pg_namespace.oid = pg_class.relnamespace,
     pg_user,
     (values('SELECT', 1),('INSERT', 2),('UPDATE', 3),('DELETE', 4)) privs(priv, privorder)
where relkind in ('r', 'v')
      and has_table_privilege(pg_user.usesysid, pg_class.oid, priv)
      and not (nspname ~ '^pg_' or nspname = 'information_schema')
order by 2, 1, 3, privorder;

取自:Find out if user got permission to select/update/... a table/function/... in PostgreSQL

如果相关,请使用alter schema和/或alter table修复权限: