PostgreSQL 9.6数据库关系权限被拒绝

时间:2018-01-23 15:15:42

标签: database postgresql docker permissions

我在创建的数据库中创建新对象(特别是视图)时遇到问题。虽然此处报告了许多类似的问题,但请参阅permission denied for relationpermission denied for sequence,但是尽管研究了已接受的答案(在某些情况下,还有PostgreSQL文档),我还是无法解决这个问题。

以下是我正在运行的脚本片段,用于创建数据库,新角色,然后更改数据库的默认权限:

-- # Uncomment line below for debugging purposes
SET client_min_messages TO debug1;

CREATE DATABASE mydatabase WITH ENCODING 'UTF8' TEMPLATE template1;

-- Create user foo
CREATE ROLE foo LOGIN ENCRYPTED PASSWORD '29829932499gd' NOINHERIT;


\c mydatabase


--- this grants privileges on new objects generated in new database "mydatabase"
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO foo; 
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO foo;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO foo;


--- import DDL from SQL files (Ommitted for brevity sake)

当我尝试创建一个物化视图时,我意识到我试图从中选择的一个表(table_1)上存在权限错误。

我直接在CLI尝试了一个简单的查询:

mydatabase=> select * from table_1;
ERROR:  permission denied for relation table_1

然后我在psql CLI(连接到 mydatabase 数据库)上手动输入了这个:

  

在SCHEMA public TO foo的所有表格中授予所有特权;

然后我得到了答复:

  

错误:关系表__2的权限被拒绝

以下是table_1和table_2的架构:

CREATE TABLE IF NOT EXISTS table_1 (
                        td                  DATE NOT NULL,

                        f1                  REAL CHECK (f1 > 0) NOT NULL ,
                        f2                  REAL CHECK (f2 > 0 and f2 >= f1 and f2 >= f3 and f2 >= f4) NOT NULL ,
                        f3                  REAL CHECK (f3 > 0 and f3 <= f1 and f3 <= f2 and f3 <= f4) NOT NULL ,
                        f4                  REAL CHECK (f4 > 0) NOT NULL,
                        f5                  BIGINT CHECK (f5 > -1) DEFAULT 0 NOT NULL,
                        f6                  BIGINT CHECK (f6 > -1) DEFAULT 0 NOT NULL,

                        moff                SMALLINT DEFAULT 0 CHECK (moff > -1) NOT NULL,

                        flg_03_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_03_l              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_05_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_05_l              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_10_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_10_l              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_20_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_20_l              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_60_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_60_l              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_52w_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_52w_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_at_h              BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_at_l              BOOLEAN DEFAULT FALSE NOT NULL,

                        flg_03v_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_03v_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_05v_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_05v_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_10v_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_10v_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_20v_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_20v_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_60v_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_60v_l             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_52wv_h            BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_52wv_l            BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_atv_h             BOOLEAN DEFAULT FALSE NOT NULL,
                        flg_atv_l             BOOLEAN DEFAULT FALSE NOT NULL,

                        ar_1w                 REAL DEFAULT -99999 NOT NULL,
                        ar_2w                 REAL DEFAULT -99999 NOT NULL,
                        ar_1m                 REAL DEFAULT -99999 NOT NULL,
                        ar_3m                 REAL DEFAULT -99999 NOT NULL,


                        ar_1w_mzs             REAL DEFAULT -99999 NOT NULL,
                        ar_2w_mzs             REAL DEFAULT -99999 NOT NULL,
                        ar_1m_mzs             REAL DEFAULT -99999 NOT NULL,
                        ar_3m_mzs             REAL DEFAULT -99999 NOT NULL,

                        PRIMARY KEY           (td, moff)
                        );

CREATE INDEX idx_tb1_flg03f2  ON table_1 (td, flg_03_h);
CREATE INDEX idx_tb1_flg03f3  ON table_1 (td, flg_03_l);
CREATE INDEX idx_tb1_flg05f2  ON table_1 (td, flg_05_h);
CREATE INDEX idx_tb1_flg05f3  ON table_1 (td, flg_05_l);
CREATE INDEX idx_tb1_flg10f2  ON table_1 (td, flg_10_h);
CREATE INDEX idx_tb1_flg10f3  ON table_1 (td, flg_10_l);
CREATE INDEX idx_tb1_flg20f2  ON table_1 (td, flg_20_h);
CREATE INDEX idx_tb1_flg20f3  ON table_1 (td, flg_20_l);
CREATE INDEX idx_tb1_flg60f2  ON table_1 (td, flg_60_h);
CREATE INDEX idx_tb1_flg60f3  ON table_1 (td, flg_60_l);
CREATE INDEX idx_tb1_flg52wf2 ON table_1 (td, flg_52w_h);
CREATE INDEX idx_tb1_flg52wf3 ON table_1 (td, flg_52w_l);
CREATE INDEX idx_tb1_flgatf2  ON table_1 (td, flg_at_h);
CREATE INDEX idx_tb1_flgatf3  ON table_1 (td, flg_at_l);

CREATE INDEX idx_tb1_flg03f5f2  ON table_1 (td, flg_03v_h);
CREATE INDEX idx_tb1_flg03f5f3  ON table_1 (td, flg_03v_l);
CREATE INDEX idx_tb1_flg05f5f2  ON table_1 (td, flg_05v_h);
CREATE INDEX idx_tb1_flg05f5f3  ON table_1 (td, flg_05v_l);
CREATE INDEX idx_tb1_flg10f5f2  ON table_1 (td, flg_10v_h);
CREATE INDEX idx_tb1_flg10f5f3  ON table_1 (td, flg_10v_l);
CREATE INDEX idx_tb1_flg20f5f2  ON table_1 (td, flg_20v_h);
CREATE INDEX idx_tb1_flg20f5f3  ON table_1 (td, flg_20v_l);
CREATE INDEX idx_tb1_flg60f5f2  ON table_1 (td, flg_60v_h);
CREATE INDEX idx_tb1_flg60f5f3  ON table_1 (td, flg_60v_l);
CREATE INDEX idx_tb1_flg52f5wf2 ON table_1 (td, flg_52wv_h);
CREATE INDEX idx_tb1_flg52f5wf3 ON table_1 (td, flg_52wv_l);
CREATE INDEX idx_tb1_flgatf5f2  ON table_1 (td, flg_atv_h);
CREATE INDEX idx_tb1_flgatf5f3  ON table_1 (td, flg_atv_l);

CREATE INDEX idx_tb1_ar_1w  ON table_1 (td, ar_1w);
CREATE INDEX idx_tb1_ar_2w  ON table_1 (td, ar_2w);
CREATE INDEX idx_tb1_ar_1m  ON table_1 (td, ar_1m);
CREATE INDEX idx_tb1_ar_3m  ON table_1 (td, ar_3m);

CREATE INDEX idx_tb1_ar_1w_mz  ON table_1 (td, ar_1w_mzs);
CREATE INDEX idx_tb1_ar_2w_mz  ON table_1 (td, ar_2w_mzs);
CREATE INDEX idx_tb1_ar_1m_mz  ON table_1 (td, ar_1m_mzs);
CREATE INDEX idx_tb1_ar_3m_mz  ON table_1 (td, ar_3m_mzs);


CREATE TABLE IF NOT EXISTS table_2  (
                                    id                  SERIAL PRIMARY KEY NOT NULL,
                                    name                TEXT NOT NULL
                            );
CREATE UNIQUE INDEX idxu_table2_nm ON table_2 (name);

可以看出, table_1 table_2 之间存在 NO 关系,所以我怀疑postgreSQL给出的错误消息实际上是红鲱鱼(即误导)。

最后一件事。我使用 Docker * 来部署我的postgreSQL服务 - 所以上面的脚本由docker运行。我运行docker run时创建了数据库 - 但是,当我以用户foo登录时,我尝试创建新视图(甚至在任何表上运行简单的SELECT),我得到错误:

  

错误:关系$ table_name

的权限被拒绝

我注意到的另一件奇怪的事情(当我在psql命令中运行\z时)显示数据库中没有任何表具有任何权限:

                                                        Access privileges
 Schema |                           Name                            |   Type   | Access privileges | Column privileges | Policies 
--------+-----------------------------------------------------------+----------+-------------------+-------------------+----------
 public | mdb_company_financials_balsheet                           | table    |                   |                   | 
 public | mdb_company_financials_cashflow                           | table    |                   |                   | 
 public | mdb_company_financials_income_stmt                        | table    |                   |                   | 
 public | mdb_ccy_group_member                                      | table    |                   |                   | 

那么,是什么导致了这个权限问题,我该如何解决呢?

*(不确定是否是相关信息)。

[[附录]

                                         version                                          
------------------------------------------------------------------------------------------
 PostgreSQL 9.6.5 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit

1 个答案:

答案 0 :(得分:1)

经过一些研究,这对我有用。 psql(9.6.12)

-- login to postgres database server as postgres user.
ssh <user>>@hostname.<domain>>.com
sudo su postgres
id
psql
postgres=# create user sentry_read with login password 'sentry_read';
postgres=# du+
postgres=# \l+
-- **very important: make sure we should connect required database before we grant to user.**
postgres=# \c sentry
postgres=# \dt
sentry=# \dp SENTRY_VERSION
sentry=# select * from "SENTRY_VERSION";
sentry=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO sentry_read;
postgres=# \q

[user@hostname ~]$ psql -U sentry_read -d sentry -h hostname.<domain>>.com -W
sentry=> select * from "SENTRY_VERSION";