postgresql Postgres报一个关系不存在,但是表存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20729625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:14:34  来源:igfitidea点击:

Postgres reports that a relation does not exist, but the table exists

postgresqlexpress

提问by jhamm

I have an expressapp that I am connecting to my Postgresdb. Here is my code:

我有一个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);
  });
});

I go to my local Postgresand look at the isxdb and here are the tables available.

我去我的本地Postgres查看isx数据库,这里有可用的表。

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

But when I try to hit the userstable I get this error Error: relation "users" does not exist.

但是当我尝试敲users桌子时,我收到了这个错误Error: relation "users" does not exist

The relation usersexists. I have checked and I am connected to the instance of Postgresthat I thought I was connected to. What else can I be missing?

关系users存在。我已经检查并连接到Postgres我认为连接到的实例。我还能缺少什么?

采纳答案by Denis de Bernardy

Check for potential permission problems, e.g. an incorrect search path, or incorrect permissions, or something to that order. You could run this query to show all available tables, for instance:

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

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

It might also be worth looking into case-sensitivity related issues. For instance, perhaps the pglibrary is adding double-quotes around lowercase identifiers, and you created tables with CamelCase, or something to that order.

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

Then check the search_pathif relevant:

然后检查search_path是否相关:

show search_path;

If the path is correct, then check permissions, for instance using:

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

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;

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

摘自:找出用户是否有权在 PostgreSQL 中选择/更新/...一个表/函数/...

If relevant, fix the permissions using alter schemaand/or alter table:

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