gpt4 book ai didi

database - PostgreSQL 9.6数据库上的关联权限被拒绝

转载 作者:行者123 更新时间:2023-12-02 18:30:25 27 4
gpt4 key购买 nike

我在创建的数据库中创建新对象(特别是 View )时遇到问题。尽管SO上报告了许多类似的问题,请参见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)

当我尝试创建实例化 View 时,我意识到尝试从中进行选择的一个表(table_1)上存在权限错误。

我直接在CLI上尝试了一个简单的查询:
mydatabase=> select * from table_1;
ERROR: permission denied for relation table_1

然后,我在psql CLI(连接到mydatabase数据库)上手动键入了以下内容:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO foo;



然后,我得到响应:

ERROR: permission denied for relation table_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用户身份登录时,尝试创建新 View (甚至在任何表上运行简单的SELECT)时,都会收到错误消息:

ERROR: permission denied for relation $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

最佳答案

经过研究后,这对我有用。 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";

关于database - PostgreSQL 9.6数据库上的关联权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48405044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com