Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter all tables postgres set owner

I need a postgresql statement to run through the public schema (all tables) and update the owner. I know the manual command is ALTER TABLE public.<table_name> OWNER TO <username>;

However I'm not sure how to put this into a script/format which will loop through each table and update the owner?

This is for a postgres 9.6 database (it will be run on a test db).

Thanks

like image 907
sc-leeds Avatar asked Aug 30 '26 15:08

sc-leeds


1 Answers

Connect to the database with psql and run

SELECT format(
          'ALTER TABLE public.%I OWNER TO user_name',
          table_name
       )
FROM information_schema.tables
WHERE table_schema = 'public'
  AND table_type = 'BASE TABLE' \gexec

\gexec will execute each line of the query result as a statement.

Do upgrade.

like image 118
Laurenz Albe Avatar answered Sep 01 '26 09:09

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!