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

相关推荐
IvorySQL5 小时前
PostgreSQL 日报|修复截断 zstd 备份检测(10 月 9 日)
数据库·postgresql
刘胡子大叔6 小时前
SQL 脚本的导入顺序
数据库·sql
代码什么用6 小时前
Spring对IoC的实现
数据库·spring
hz567898 小时前
涉密视频会议设备配置指南:终端、音视频采集与配套设施选型
服务器·网络·数据库·安全·实时音视频·信息与通信·智能硬件
广州浮点FLOATLIC8 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
程序员Sunday8 小时前
MySQL 为什么使用 B+ 树索引?把范围查询、回表和覆盖索引连起来
数据库·mysql
半杯咖啡半行码9 小时前
Qt开发实战:数据库、MV 模式、QProcess与串口通信全攻略
数据库·qt
小米里的大麦9 小时前
16 MySQL 事务
数据库·mysql
西柚小萌新9 小时前
【LLM&&AI应用开发 八股文】--4.3.Agent智能体(下)
java·开发语言·数据库
邓工说电10 小时前
智慧断路器安全吗?数据加密、离线保护与合规认证全解读
大数据·数据库·人工智能·智能断路器·炜晔科技