Skip to content

Postgres: fix incorrect view table ownership

Stable TMS databases have view tables with the wrong owner (https://app.clickup.com/t/86c5cne6u) The only ways to fix specifically view table ownership are to either have the current owner of the view table run a query to transfer the ownership of the view table or delete the view table and re-create it.

How to change ownership

-- Baseline, what do the table permissions look before they have been modified?
\dp <table-name>
BEGIN ;
ALTER VIEW <table-name> OWNER TO "<new-owner>";
-- Comparison, what do the table permissions look like now when compared to the baseline?
\dp <table-name>

Everything looks good? Great, COMMIT ; away!

Change ownership of view tables in bulk

List the view tables in your ownership:

SELECT viewname
FROM pg_views
WHERE viewowner = '<your-first-lastname>@aholadigital.com';

Create a bulk query from each view table name like so:

ALTER VIEW <table-name> OWNER TO "<new-owner>";
BEGIN ;
ALTER VIEW customer_transport_account_1dfccc44 OWNER TO "tms_reports";
ALTER VIEW trip_bill_accounting_report_data_1dfccc44 OWNER TO "tms_reports";
ALTER VIEW trip_offers_data_1dfccc44 OWNER TO "tms_reports";
...

Check out the new owner with \d+ for full listing or \dp <table-name> for a single table. Looks good? You can commit.

COMMIT ;