下面我正在创建数据库 mydb
并填充它。请注意,我执行的最后一步是为 postgres
设置密码。这只是为了避免在之前的步骤中出现密码提示。
我按照其他 StackOverflow
帖子中的步骤操作,即在 GRANT ALL
、TABLES
和 SEQUENCES
上发布 FUNCTIONS
,但仍然面临以下问题.
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
脚本没有失败,但我也无法以 mydb
的身份访问 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=>
SIDEBAR:我注意到 some_table
的所有者是 postgres
,并且希望它是 user01
。也许这可能是问题的一部分。
我错过了什么?
提前致谢。
EDIT:请注意,我也尝试在 psql --dbname=mydb --username=postgres -f /tmp/mydb.sql
语句之前运行 GRANT ALL
。
答案 0 :(得分:2)
您授予对数据库 postgres 中公共模式中的表、序列和函数的权限,而不是 mydb。默认情况下,psql 将连接到与当前用户同名的数据库,在本例中为 postgres。确保通过将 C:\Users\excel_outputs\Solvedsheet1
添加到 psql 命令中来运行 mydb 中的命令。