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

相关推荐
Decay__6 小时前
05_MySQL 常用 SQL 大全 —— 诊断、管理、优化辅助 SQL 与三大系统库速查
运维·数据库·sql·mysql·架构
小徐不徐说10 小时前
C++ 接口与插件设计:从工厂注册到 Qt 动态插件
数据库·c++·qt·系统架构·程序设计
论文复现现场11 小时前
WRF、FEA 能用 L40S 加速吗?求解器检查、断点恢复与长期运行
java·数据库·算法·wrf·有限元分析·gpu计算·断点续算
Decay__12 小时前
04_MySQL DBA 日常运维手册 —— 会话管理、锁与阻塞、事务监控、慢查询与日志巡检
运维·数据库·sql·mysql·架构
心梦缘-雪雁13 小时前
SQL Server 查询数据存在哪个表里
数据库
lusklusklusk14 小时前
Python 巡检脚本:进程运行状态、数据库写入时效检查及邮件通知与告警
数据库·python·oracle
GeW14 小时前
产业新优势基础软件的人才底盘,从Linux与数据库开始
linux·数据库
SelectDB14 小时前
Doris Arrow Flight SQL 高速读取协议实战:与 Snowflake 客户端取数链路对照
大数据·数据库·数据分析
曼岛_14 小时前
[图解密码学]第4章 古典密码
java·数据库·密码学
瀚高PG实验室15 小时前
HGDB V9.0.5 .pgpass 免密登录提示 IsEncryptNon-base64 encoding 警告处理
数据库·postgresql·瀚高数据库