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

相关推荐
SelectDB10 小时前
为什么越来越多分析数据库开始重视实时更新,而不仅仅是查询性能?
大数据·数据库·数据分析
SelectDB10 小时前
为什么今天值得重新比较 Apache Doris 和 StarRocks?
大数据·数据库·数据分析
l12586510 小时前
# LangGraph Deep Research Agent 全流程设计:多轮研究、人机协同与真实来源管理
数据库·人工智能·python·算法·自然语言处理·oracle·langchain
Frank_refuel10 小时前
MYSQL【进阶】 -> 索引(了解)
数据库·mysql
SelectDB11 小时前
开源!Apache Doris 上线 Profile 可视化诊断:基于 Doris Skills 破解 AI 误诊难题
数据库·agent·自动化运维
DBA_G11 小时前
南大通用GBase 8s数据库的“一库三模“
数据库
l1t11 小时前
DeepSeek总结的DuckDB 如何更快地运行递归 CTE
java·开发语言·数据库·mysql·duckdb
Baron X12 小时前
Redis 7.0.9 完整部署指南
数据库·redis
weixin_4407305012 小时前
数据库缓存、索引、缓存命中率的理解
数据库·缓存
DeepAgent12 小时前
AI Agent 工程实践(34):企业 AI Agent 架构
数据库·人工智能·agent