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

相关推荐
Hrain-AI6 小时前
多 Agent 并行不打架:worktree 隔离与反馈回流落地(附脚本)
网络·数据库·人工智能·架构
净水深流7 小时前
中央厨房冷链技术实践:多温区改造、WMS落地与IoT温控架构
大数据·数据库·人工智能·冷库冷链
l1t7 小时前
测试DuckDB 2.1的match_recognize模式匹配语句
数据库·duckdb
IpdataCloud8 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
曹牧8 小时前
Java:SQL 注入漏洞
数据库
程序员JerrySUN9 小时前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】
java·数据库·redis·安全·mybatis
oradh9 小时前
Oracle Undo问题总结(ORA-600 [4xxx] 系列错误)
数据库·oracle
达梦数据10 小时前
DMDRS辅助表生成规则与作用
数据库·oracle
‎ദ്ദിᵔ.˛.ᵔ₎10 小时前
MySQL 表约束
数据库·mysql
Doris__HE10 小时前
【元脑服务器NF5476G7-NF5476M7技术规格分享】
运维·服务器·网络·数据库·性能优化