我正在使用PostGres 9.5。我在将数据插入到刚创建的表中时遇到问题。尽管向数据库用户授予了所有权限,但我收到了“权限被拒绝”错误。见下文......
localhost:myapp davea$ psql -Upostgres
Password for user postgres:
psql (9.5.0, server 9.5.1)
Type "help" for help.
postgres=# GRANT ALL ON schema public TO myapp;
GRANT
localhost:myapp davea$ psql -Upostgres
Password for user postgres:
psql (9.5.0, server 9.5.1)
Type "help" for help.
postgres=# GRANT USAGE ON schema public TO myapp;
GRANT
postgres=# \q
localhost:myapp davea$ myapp
psql (9.5.0, server 9.5.1)
Type "help" for help.
myapp=> insert into search_codes (id, code, address_id) values (1, 'atlanta', 'GA');
ERROR: permission denied for relation search_codes
myapp=> select * FROM search_codes;
ERROR: permission denied for relation search_codes
myapp=> \d search_codes;
Table "public.search_codes"
Column | Type | Modifiers
------------+-------------------+-----------------------------------------------------------
id | integer | not null default nextval('search_codes_id_seq'::regclass)
code | character varying |
address_id | character varying |
Indexes:
"search_codes_pkey" PRIMARY KEY, btree (id)
"index_search_codes_on_code" UNIQUE, btree (code)
"index_search_codes_on_address_id" btree (address_id)
授予权限的正确方法是什么,以便我的用户可以从表中插入和选择?
答案 0 :(得分:2)
你的第一个命令让你能够列出表格(你可以知道存在)
GRANT USAGE ON SCHEMA public TO myapp
然后你必须向schema public
中的所有表授予SELECT,INSERT等GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA public TO myapp
我建议不要为特定应用提供所有权限。
如果你有序列:
GRANT SELECT, UPDATE, USAGE ON ALL SEQUENCES IN SCHEMA public to myapp
如果你有功能:
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO myapp
然后你的例子就可以了。
但如果您希望能够访问未来创建的表,您仍然需要应用一些命令:
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, UPDATE, INSERT, DELETE, TRIGGER ON TABLES TO myapp
Postgresql有一个非常奇怪的机制,我花了一段时间来理解它!
答案 1 :(得分:1)
您在postgres数据库而不是myapp数据库中授予权限。
将第一个PSQL命令更改为
psql -Upostgres myapp
然后发出补助金
答案 2 :(得分:1)
看看https://dba.stackexchange.com/questions/33943/granting-access-to-all-tables-for-a-user。
我认为您的问题是GRANT对于数据库,模式和表是不同的,因此您需要明确地设置表的权限。
答案 3 :(得分:1)
首先,您必须使用postgres用户登录新数据库
psql your_db_name -U postgres -h your_host_name
具有列出表的功能
GRANT USAGE ON SCHEMA public TO your_user_name
然后,您必须授予模式public中所有表的权限
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user_name
如果您有序列,请给予许可
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO your_user_name