PostgreSQL 13 :: 错误:表“mydb”的权限被拒绝

NYC*_*yes 1 postgresql ddl psql

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

我按照其他帖子中的步骤操作,即在、和上StackOverflow发出s ,但仍然面临以下问题。GRANT ALLTABLESSEQUENCESFUNCTIONS

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

Run Code Online (Sandbox Code Playgroud)

该脚本不会失败,但我也无法访问mydbas user01

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=>
Run Code Online (Sandbox Code Playgroud)

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

我缺少什么?

先感谢您。

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

Jer*_*emy 7

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