<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>object ownership &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/object-ownership/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 20 Nov 2017 05:55:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>How to change ownership for all objects in PostgreSQL</title>
		<link>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/</link>
					<comments>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 23 May 2015 16:51:25 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[object ownership]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[select]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=191</guid>

					<description><![CDATA[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. [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fhow-to-change-ownership-for-all-objects%2F&#038;title=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" data-a2a-url="https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/" data-a2a-title="How to change ownership for all objects in PostgreSQL"></a></p><p>In this post, I am sharing a script which we can use for changing ownership of all objects in PostgreSQL.</p>
<p><b>Why ownership of object needs to change?</b></p>
<p>I have created one application user for our production PostgreSQL database.</p>
<p>But system admin and deployment manager made one mistake and executed objects with some another database user.</p>
<p>After deployment, we came to know that objects were not deployed under proper ownership.</p>
<p>Now we have 2500 tables, 450 views and 230 stored functions are required to change their ownership.</p>
<p>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.</p>
<p><b>Below is my demonstration:</b></p>
<p>Let&#8217;s First, create one function with $parameter value so that you can use this function as a template.</p><pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION ChangeObjectsOwnerShip(text) 
returns text language plpgsql volatile
  AS $f$
    BEGIN
      EXECUTE $1;
      RETURN $1;
    END;
$f$;</pre><p>
Now generate two select statements to get the alter script.</p>
<p><b>Below select statement is for Tables, Sequences and Views.</b></p><pre class="crayon-plain-tag">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 &lt;&gt; 'information_schema' 
  AND relkind IN ('r','S','v') ORDER BY relkind = 'S'
)s;</pre><p>
<b>Below select statement is for all functions. </b></p><pre class="crayon-plain-tag">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;</pre><p>
In above code, preparing one alter statement to change ownership for all objects from assign user to mentioned &#8221; <b>app_user</b> &#8220;. You can change <b>&#8221; app_user &#8221;  </b>by your require user name.</p>
<p>After executing above select statements, you can find all alter scripts in a result.</p>
<p>Copy this result and execute above alter scripts using super user.</p>
<p>All ownership will be changed after executing this alter the script.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
