Best way to change the ownership of a PostgreSQL database and its tables?

I am trying to change the owner of a PostgreSQL database (version> 8.2) and its tables.

I read this solution:

Change OWNER in all tables at once in PostgreSQL

But is this the best way to do this (for the latest versions of PostgreSQL) ?. It seems like there is a REASSIGN OWNED function which is better, but this one changes every database owned by old_role, doesn't it? I only want it for one database.

Like this post:

REASSIGN OWNED BY for 1 specified database

I'm not going to change the postgres owner, what is the best way these days?

Thank you in advance

+4
source share
2 answers

According to the manual:

REASSIGN OWNED ,

, , , , ( ).

SO, , postgres, . .

+10

, spring :

1) , , , script, .

ALTER DATABASE <dbname> OWNER TO <newowner>;
\o altertable.sql
SELECT 'ALTER TABLE ' || table_name || ' OWNER TO <newowner>; ' FROM information_schema WHERE table_schema = <oldowner> and table_catalog = '<dbname>';
\o
\i altertable.sql

, .

2) pg_dump , :

pg_dump -Fp -f dbbackup.dmp <dbname>
vi dbbackup.dmp
:%s/oldowner/newowner/g
save and exit

, .

+5

Source: https://habr.com/ru/post/1542477/


All Articles