How to solve permission denied for schema public

在使用psql时遇到permissiondeniedforschemapublic错误,用户sam需要被授予publicschema的权限。通过切换到postgres超级用户,执行grantallonschemapublictosam,然后用户sam可以成功创建表并进行数据插入。新用户需赋予相应权限才能执行数据库的CRUD操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.  ERROR: permission denied for schema public

C:\Users\Maxwell Pan>psql -U sam -d nano
Password for user sam:
psql (15.2)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

nano=> create table tree(type varchar(100));
ERROR:  permission denied for schema public
LINE 1: create table tree(type varchar(100));
                     ^
nano=>

2. 切换到postgres超级用户下,进行授权。

nano=> \c nano postgres
Password for user postgres:
You are now connected to database "nano" as user "postgres".
nano=# grant all on schema public to sam;
GRANT
nano=#

3. 切换到 sam用户下进行建表。

nano=# \c nano sam
Password for user sam:
You are now connected to database "nano" as user "sam".
nano=> \d
Did not find any relations.
nano=> create table tree(type varchar(100));
CREATE TABLE
nano=> \d
       List of relations
 Schema | Name | Type  | Owner
--------+------+-------+-------
 public | tree | table | sam
(1 row)


nano=> insert into tree values('this is a olive tree');
INSERT 0 1
nano=>

4. 总结思考:

对于新建立的用户,需要给与相关权限才可以进行CRUD。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值