【问题标题】:PostgreSQL 13 :: ERROR: permission denied for table 'mydb'PostgreSQL 13 :: 错误:表'mydb'的权限被拒绝
【发布时间】:2021-04-08 22:59:14
【问题描述】:

下面我正在创建数据库mydb 并填充它。请注意,我执行的最后一步是为postgres 设置密码。这只是为了避免在前面的步骤中提示密码。

我按照其他StackOverflow 帖子中的步骤操作,即在TABLESSEQUENCESFUNCTIONS 上发布GRANT ALLs,但我仍然面临以下问题。

mydb.sh

su - postgres <<xEOFx
set +H

psql -c "CREATE DATABASE mydb"
psql -c "CREATE USER user01 WITH ENCRYPTED PASSWORD 'SomePassword'"

psql -c "GRANT ALL PRIVILEGES ON ALL TABLES    IN SCHEMA public to user01"
psql -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public to user01"
psql -c "GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public to user01"

psql --dbname=mydb --username=postgres -f /tmp/mydb.sql
psql -c "GRANT ALL PRIVILEGES ON DATABASE mydb TO user01"

psql -c "ALTER USER postgres WITH PASSWORD 'AnotherPassword'"
exit
xEOFx

脚本没有失败,但我也无法以user01 的身份访问mydb

jdoe$ psql --username=user01 --dbname=mydb
Password for user user01: 
psql (13.2)
Type "help" for help.

mydb=> \c
You are now connected to database "mydb" as user "user01".
mydb=> \dt
             List of relations
 Schema |     Name      | Type  |  Owner   
--------+---------------+-------+----------
 public | some_table    | table | postgres
(1 rows)

mydb=> select * from some_table;
ERROR:  permission denied for table some_table
mydb=>

SIDEBAR:我确实注意到some_table 的所有者是postgres,并且希望它是user01。也许这可能是问题的一部分。

我错过了什么?

提前谢谢你。

编辑:请注意,我也尝试在 GRANT ALL 语句之前运行 psql --dbname=mydb --username=postgres -f /tmp/mydb.sql

【问题讨论】:

    标签: postgresql psql ddl


    【解决方案1】:

    您正在向数据库 postgres 中的公共模式中的表、序列和函数授予权限,而不是 mydb。默认情况下,psql 将连接到与当前用户同名的数据库,在本例中为 postgres。确保通过将 -d mydb 添加到 psql 命令来运行 mydb 中的命令。

    【讨论】:

    • 我明白了。 UpVoted。让我根据您的建议重新安排事情并重试。如果可行,我将选择此作为答案。马上回来。 LoL。谢谢。
    • 这是您指出的内容和./pg_hba.conf 问题的组合,但两者都是必要的。谢谢你的信息。
    猜你喜欢
    • 2018-07-19
    • 2017-04-04
    • 2013-01-25
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2016-12-13
    • 2022-01-24
    相关资源
    最近更新 更多