无论如何,都无法获得关系的权限

时间:2017-03-02 07:38:13

标签: postgresql

我已尝试过几乎所有事情来获取postgres中某些关系的用户权限。以下是我尝试过的内容:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO myuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO myuser;

我已将我的pg_hba.cnf更改为:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

我确定这些命令可以做一些事情,如果你正确地纠正它们,但为什么这些命令中的任何一个都没有做任何接近他们所说的任何事情?

我正在使用python:

>>> conn = psycopg2.connect(host="localhost", port="5432", dbname="mydb",user="myuser", password="mypassword")
>>> cur = conn.cursor()
>>> cur.execute('select count(*) from mytable')

这是我得到的错误:

>>> cur.execute('select count(*) from public.mytable')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: permission denied for relation mytable

我也尝试过:

ALTER DATABASE mydb OWNER TO myuser;

然后重新启动。

3 个答案:

答案 0 :(得分:1)

您是否在myuser分配了所有者mydb? 这是解决问题的简单方法:

CREATE DATABASE mydb OWNER myuser;

答案 1 :(得分:1)

所以根据this回答,我可以通过单独授予一个表来获取访问权限,我想我必须对每个表进行访问。

仍然不确定为什么会这样。

GRANT ALL PRIVILEGES ON TABLE mytable TO myuser;

而这些没有。

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO myuser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO myuser;

答案 2 :(得分:1)

ALTERT DEFAULT PRIVILEGES周围似乎有一些变化。在我的情况下

ALTER DEFAULT PRIVILEGES GRANT ALL ON TABLES TO myuser;

工作了。我还必须包含schema

-- change PUBLIC to appropriate schema
ALTER DEFAULT PRIVILEGES IN SCHEMA PUBLIC GRANT ALL ON TABLES TO myuser;