postgres database 更换用户

Say, that database "foo" is now owned by "user1"

Alter owner:

复制代码
\c postgres
ALTER DATABASE "foo" OWNER TO "user2"

Reassign ownership (see also https://stackoverflow.com/a/13535184/1943126):

note: this will affect objects in current database ("foo") but also shared objects (databases,tablespaces)!!!

复制代码
\c "foo"
REASSIGN OWNED BY "user1"  TO "user2" 

note: the following is probably not needed, covered by re-assignment of ownership

Revoke existing permissions from previous owner on all available schemas:

复制代码
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM "user1";
REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA "bar" FROM "user1";
REVOKE USAGE ON SCHEMA "bar" FROM "user1";

Grant permissions on new owner:

复制代码
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "user2";
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA "bar" TO "user2";
GRANT USAGE ON SCHEMA "bar" TO "user2"

Check existing permissions on tables:

复制代码
SELECT grantor, grantee, table_schema, table_name,privilege_type FROM information_schema.table_privileges
复制代码
REASSIGN OWNED BY old_role [, ...] TO new_role

This changes all objects owned by old_role to the new role. You don't have to think about what kind of objects that the user has, they will all be changed.

Note that it only applies to objects inside a single database. It does not alter the owner of the database itself either.

It is available back to at least 8.2. Their online documentation only goes that far back.

------单个DB 不是所有 REASSIGN OWNED BY doesn't work for objects owned by postgres.

Just to be clear, this command works in the database that you are currently connected ONLY. If the old_role owns objects in multiple databases, you should connect and run this command in each one of those databases

Since you're changing the ownership for all tables, you likely want views and sequences too. Here's what I did:

Tables:

复制代码

for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" YOUR_DB` ; do psql -c "alter table \"$tbl\" owner to NEW_OWNER" YOUR_DB ; done

Sequences:

复制代码

for tbl in `psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" YOUR_DB` ; do psql -c "alter sequence \"$tbl\" owner to NEW_OWNER" YOUR_DB ; done

Views:

复制代码

for tbl in `psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" YOUR_DB` ; do psql -c "alter view \"$tbl\" owner to NEW_OWNER" YOUR_DB ; done

You could probably DRY that up a bit since the alter statements are identical for all three

相关推荐
無a伟2 小时前
redis缓存完整详解
数据库·redis·缓存
G31135422733 小时前
大模型不可用时,业务还能不能继续:企业需要设计降级方案
大数据·服务器·数据库·人工智能·深度学习
VALENIAN瓦伦尼安教学设备4 小时前
激光对中仪采购要点
数据库·嵌入式硬件·算法
码农颜4 小时前
5.2.2 隔离级别
数据库·mysql
微三云 - 廖会灵 (私域系统开发)4 小时前
基于 OPC 流程控制的抖店集群 SaaS 平台设计与渠道分账系统实现
数据库
骇客野人4 小时前
MySQL 信创化迁移至 PolarDB-X 整体实施方案
数据库·mysql
计科土狗5 小时前
GESP六级专题之类与对象
java·前端·数据库
麦聪聊数据6 小时前
数据治理 ROI(下):算清投入产出比,小切口落地快速验证价值
数据库
dyxal6 小时前
SSH本地端口转发完全解析:像“挖掘隧道”一样安全访问远程数据库
数据库·安全·ssh