postgresql 数据库所有者无法访问数据库 - “未找到关系。”

发布于 2024-12-09 20:13:00 字数 2132 浏览 0 评论 0原文

我有一个用户:user_x,它在 postgresql 上拥有一个数据库,并且没有任何 ROLE 属性,例如(CREATE_DB,SUPERUSER,...)

这个 user_x 可以访问整个数据库,创建表(在他的数据库上),选择,插入并更新数据。

我有这个数据库列表:

mydatabase=> \l
                                     List of databases
          Name           |  Owner   | Encoding  | Collation | Ctype |   Access privileges   
-------------------------+----------+-----------+-----------+-------+-----------------------
 postgres                | postgres | SQL_ASCII | C         | C     | 
 mydatabase              | user_x   | UTF8      | C         | C     | 
 template0               | postgres | SQL_ASCII | C         | C     | =c/postgres          +
                         |          |           |           |       | postgres=CTc/postgres
 template1               | postgres | SQL_ASCII | C         | C     | =c/postgres          +
                         |          |           |           |       | postgres=CTc/postgres
 whoami                  | postgres | SQL_ASCII | C         | C     | 
(6 rows)

以及以下角色:

mydatabase=> \du
                       List of roles
 Role name |            Attributes             | Member of 
-----------+-----------------------------------+-----------
 postgres  | Superuser, Create role, Create DB | {}
 user_x    |                                   | {}

mydatabase=> \d
                        List of relations
 Schema |               Name                |   Type   |  Owner   
--------+-----------------------------------+----------+----------
 public | addresses                         | table    | user_x
 public | addresses_id_seq                  | sequence | user_x
 public | assignments                       | table    | user_x
 public | assignments_id_seq                | sequence | user_x

 ...

好吧,直到我转储数据并将其恢复到另一个 postgresql 服务器上。

在另一台服务器上导入数据(具有相同的数据库名称和用户)并登录 psql 后,\d 命令回复:“未找到关系。”

所以我添加了在导入的数据库服务器上为 user_x 授予 SUPERUSER 角色,这样 user_x 就可以再次看到关系和数据。

但 user_x 不需要具有 SUPERUSER 权限即可访问该数据库。

这个导入的转储有什么问题? 现在有人如何解决这个问题吗?

I've got an user: user_x that owns a database on postgresql and does not have any ROLE attribute like (CREATE_DB, SUPERUSER,...)

This user_x can access the whole DB, create tables (on his database), select, insert and update data.

I've got this list of databases:

mydatabase=> \l
                                     List of databases
          Name           |  Owner   | Encoding  | Collation | Ctype |   Access privileges   
-------------------------+----------+-----------+-----------+-------+-----------------------
 postgres                | postgres | SQL_ASCII | C         | C     | 
 mydatabase              | user_x   | UTF8      | C         | C     | 
 template0               | postgres | SQL_ASCII | C         | C     | =c/postgres          +
                         |          |           |           |       | postgres=CTc/postgres
 template1               | postgres | SQL_ASCII | C         | C     | =c/postgres          +
                         |          |           |           |       | postgres=CTc/postgres
 whoami                  | postgres | SQL_ASCII | C         | C     | 
(6 rows)

and the following roles:

mydatabase=> \du
                       List of roles
 Role name |            Attributes             | Member of 
-----------+-----------------------------------+-----------
 postgres  | Superuser, Create role, Create DB | {}
 user_x    |                                   | {}

mydatabase=> \d
                        List of relations
 Schema |               Name                |   Type   |  Owner   
--------+-----------------------------------+----------+----------
 public | addresses                         | table    | user_x
 public | addresses_id_seq                  | sequence | user_x
 public | assignments                       | table    | user_x
 public | assignments_id_seq                | sequence | user_x

 ...

All right, till I dump data and restore it on another postgresql server.

After import the data with on another server (with same database name and user) and logged on psql the \d command reply with: "No relations found."

So I added SUPERUSER role to user_x on the imported database server and tadã user_x can see the relations and data again.

But user_x don't need to have SUPERUSER privilege to access this database.

What's wrong with this imported dump?
Does anyone now how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人如玉 2024-12-16 20:13:00

也许 public 模式的模式权限被破坏了。两个站点上 \dn+ 的输出是什么?

输出应如下所示:

                          List of schemas
  Name  |  Owner   |  Access privileges   |      Description       
--------+----------+----------------------+------------------------
 public | postgres | postgres=UC/postgres | standard public schema
                   : =UC/postgres           
(1 row)

如果 =UC/postgres 部分丢失,您可以使用以下命令恢复它

grant all on schema public to public;

Perhaps the schema permissions for the public schema got mangled. What is the output of \dn+ on both sites?

The output should look like this:

                          List of schemas
  Name  |  Owner   |  Access privileges   |      Description       
--------+----------+----------------------+------------------------
 public | postgres | postgres=UC/postgres | standard public schema
                   : =UC/postgres           
(1 row)

If the =UC/postgres part is missing, you can restore it with

grant all on schema public to public;
天煞孤星 2024-12-16 20:13:00

没找到什么关系。

如果您的数据库没有表,您也会得到这个。

iripmu=# select * from testtable;
 id |    Name
----+------------
  1 | Bear
  2 | Tiger
(2 rows)

iripmu=# drop table if exists testtable;
DROP TABLE
iripmu=# \dt
Did not find any relations.

Did not find any relations.

You will also get this if your database has no tables.

iripmu=# select * from testtable;
 id |    Name
----+------------
  1 | Bear
  2 | Tiger
(2 rows)

iripmu=# drop table if exists testtable;
DROP TABLE
iripmu=# \dt
Did not find any relations.
年华零落成诗 2024-12-16 20:13:00

我正在使用 psql 16.0,服务器 15.1。我在 DataGrip 中重命名了一个连接,现在我看不到任何表(即使在恢复到以前的名称之后)。知道如何显示表格吗?

postgres=# \dn+
                                       List of schemas
  Name  |       Owner       |           Access privileges            |      Description       
--------+-------------------+----------------------------------------+------------------------
 public | pg_database_owner | pg_database_owner=UC/pg_database_owner+| standard public schema
        |                   | =U/pg_database_owner                   | 

I am using psql 16.0, server 15.1. I renamed a connection in DataGrip and now I can't see any of the tables (even after reverting back to the previous name). Any idea how can I show the tables?

postgres=# \dn+
                                       List of schemas
  Name  |       Owner       |           Access privileges            |      Description       
--------+-------------------+----------------------------------------+------------------------
 public | pg_database_owner | pg_database_owner=UC/pg_database_owner+| standard public schema
        |                   | =U/pg_database_owner                   | 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文