Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 May How to change ownership for all objects in PostgreSQL

How to change ownership for all objects in PostgreSQL

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a script which we can use for changing ownership of all objects in PostgreSQL.

Why ownership of object needs to change?

I have created one application user for our production PostgreSQL database.

But system admin and deployment manager made one mistake and executed objects with some another database user.

After deployment, we came to know that objects were not deployed under proper ownership.

Now we have 2500 tables, 450 views and 230 stored functions are required to change their ownership.

Now, I need to solve this by using only one script because of manual changing ownership of this many objects will take too much time.

Below is my demonstration:

Let’s First, create one function with $parameter value so that you can use this function as a template.

1
2
3
4
5
6
7
8
CREATE OR REPLACE FUNCTION ChangeObjectsOwnerShip(text)
returns text language plpgsql volatile
AS $f$
BEGIN
EXECUTE $1;
RETURN $1;
END;
$f$;

Now generate two select statements to get the alter script.

Below select statement is for Tables, Sequences and Views.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT ChangeObjectsOwnerShip
(
'ALTER TABLE ' || quote_ident(s.nspname) || '.' ||quote_ident(s.relname) || ' owner TO app_user'
)
FROM
(
SELECT
nspname
,relname
FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid)
WHERE nspname NOT LIKE E'pg\\_%'
AND nspname <> 'information_schema'
AND relkind IN ('r','S','v') ORDER BY relkind = 'S'
)s;

Below select statement is for all functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT ChangeObjectsOwnerShip
(
'ALTER FUNCTION ' || quote_ident(s.nspname) || '.' ||quote_ident(s.function_name) || '('||s.parms||') owner TO app_user'
)
FROM
(
SELECT
nspname
,proname AS function_name
, pg_catalog.oidvectortypes(proargtypes) AS parms
FROM pg_catalog.pg_proc AS c
JOIN pg_namespace n
ON (c.pronamespace = n.oid)
WHERE nspname != 'information_schema'
AND nspname NOT LIKE E'pg\\_%'
ORDER BY proname
)s;

In above code, preparing one alter statement to change ownership for all objects from assign user to mentioned ” app_user “. You can change ” app_user ” by your require user name.

After executing above select statements, you can find all alter scripts in a result.

Copy this result and execute above alter scripts using super user.

All ownership will be changed after executing this alter the script.

May 23, 2015Anvesh Patel
PostgreSQL [Video]: Cross Database Queries using DbLink ExtensionPostgreSQL Paging Query using LIMIT and OFFSET
Comments: 1
  1. Rutha
    January 15, 2016 at 11:31 pm

    Someone essentially help to make seriously posts I would state. This is the first time I frequented your web page and thus far? I surprised with the research you made to create this particular publish amazing. Wonderful job!

Anvesh Patel
Anvesh Patel

Database Engineer

May 23, 2015 PostgreSQLalter, Anvesh Patel, database research and development, object ownership, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Programming, PostgreSQL Tips and Tricks, select
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....