<?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>alter &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/alter/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>PostgreSQL: Generate ALTER statements to Rename table and column name in Lower case</title>
		<link>https://www.dbrnd.com/2017/07/postgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 05 Jul 2017 16:16:18 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[RENAME]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5574</guid>

					<description><![CDATA[In this post, I am sharing a script to generate ALTER statements to rename all PostgreSQL table names and column names in lower case. Many times, I have found that PostgreSQL database developers are creating their object using pg admin GUI explorer. It by default adds a double quote to differentiate identifier. For example, table [&#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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&amp;linkname=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" 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%2F2017%2F07%2Fpostgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive%2F&#038;title=PostgreSQL%3A%20Generate%20ALTER%20statements%20to%20Rename%20table%20and%20column%20name%20in%20Lower%20case" data-a2a-url="https://www.dbrnd.com/2017/07/postgresql-generate-alter-statements-to-rename-table-and-column-name-in-lower-case-sensitive/" data-a2a-title="PostgreSQL: Generate ALTER statements to Rename table and column name in Lower case"></a></p><p>In this post, I am sharing a script to generate ALTER statements to rename all PostgreSQL table names and column names in lower case. </p>
<blockquote><p>Many times, I have found that PostgreSQL database developers are creating their object using pg admin GUI explorer. It by default adds a double quote to differentiate identifier. </p></blockquote>
<p>For example, table name like &#8220;TableName&#8221; is a difference then &#8220;tablename&#8221;. </p>
<blockquote class="wp-embedded-content" data-secret="0LuYYoBd50"><p><a href="https://www.dbrnd.com/2015/10/postgresql-why-identifiers-are-case-sensitive/">PostgreSQL: Why identifiers are Case-Sensitive</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/10/postgresql-why-identifiers-are-case-sensitive/embed/#?secret=0LuYYoBd50" data-secret="0LuYYoBd50" width="600" height="338" title="&#8220;PostgreSQL: Why identifiers are Case-Sensitive&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>In one of our project, I found this kind of object names so prepared below script to ALTER object name into Lowercase which made all object name case insensitive.</p>
<p><strong>Generate ALTER script to rename all tables into lower case letters:</strong></p>
<pre class="crayon-plain-tag">SELECT 'alter table "'||c.relname||'" rename to '||lower(c.relname)||';'
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind ='r'
      AND n.nspname='public'
ORDER BY 1;</pre> </p>
<p><strong>Generate ALTER script to rename all columns into lower case letters:</strong></p>
<pre class="crayon-plain-tag">SELECT
        'alter table "'||c.relname||'" rename "'||a.attname||'" to '||lower(a.attname)||';'
FROM pg_class c
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelid
	AND r.conname = a.attname
WHERE c.relnamespace = (select oid from pg_namespace where nspname='schema_name')
	AND a.attnum &gt; 0 AND c.relkind ='r'      
ORDER BY a.attnum</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: Increase the Performance of CREATE INDEX and DROP INDEX for InnoDB</title>
		<link>https://www.dbrnd.com/2016/10/mysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 14 Oct 2016 16:16:04 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Increase Index Performance]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3741</guid>

					<description><![CDATA[In MySQL 5.5 and higher version, Index performance has improved a lot. MySQL InnoDB engine has basically two types of index, Primary clustered index and Secondary indexes. When we are adding or dropping a clustered index, It is copying the data, and creating a new copy of the table. Yes, this is the basic nature [&#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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&amp;linkname=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" 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%2F2016%2F10%2Fmysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command%2F&#038;title=MySQL%3A%20Increase%20the%20Performance%20of%20CREATE%20INDEX%20and%20DROP%20INDEX%20for%20InnoDB" data-a2a-url="https://www.dbrnd.com/2016/10/mysql-increase-the-performance-of-create-index-and-drop-index-for-innodb-merge-index-alter-table-command/" data-a2a-title="MySQL: Increase the Performance of CREATE INDEX and DROP INDEX for InnoDB"></a></p><p>In MySQL 5.5 and higher version, Index performance has improved a lot.<br />
MySQL InnoDB engine has basically two types of index, Primary clustered index and Secondary indexes. </p>
<blockquote><p>When we are adding or dropping a clustered index, It is copying the data, and creating a new copy of the table.<br />
Yes, this is the basic nature of any clustered index because It also contains the data value. </p></blockquote>
<p><strong>But, what about Secondary Index of MySQL InnoDB ?</strong></p>
<blockquote><p>When we are adding or dropping a secondary index, internally It scans the table, and sorts the rows using memory buffers in order by the values of the secondary index key columns.</p></blockquote>
<blockquote><p>Once a CREATE INDEX or ALTER TABLE statement starts to create InnoDB secondary index, we can only read the table, but cannot update the table. </p></blockquote>
<blockquote><p>Now Imagine, that we have a table with billions of data and now we require to add few more secondary indexes.<br />
I have noticed that, most of the database developers create individual CREATE INDEX statement and execute each for creating require indexes. </p></blockquote>
<blockquote><p>We should not do this way because every CREATE INDEX statement require clustered index scan and other sorting related operation which actually degrade the performance of CREATE or DROP secondary index. </p></blockquote>
<blockquote><p>If we are adding or dropping multiple Indexes at a time, We can increase the performance of CREATE INDEX and DROP INDEX by executing using one single ALTER TABLE command. </p></blockquote>
<blockquote><p>If we are dealing with bulk-insert operation, we should create require indexes after data insertion which increase the performance of bulk-insertion. </p></blockquote>
<p><strong>For you guys, small demonstration to add or drop multiple indexes using one ALTER COMMAND. </strong></p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Employees 
(
	EmpID INT PRIMARY KEY
	,EmpName VARCHAR(50)    
	,EmpSalary INT
	,EmpGrade CHAR(1)
);</pre> </p>
<p><strong>Insert few sample records:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Employees VALUES 
(1,'Anvesh',90000,'A'),(2,'Neevan',120000,'B')
,(3,'Roy',60000,'B'),(4,'Mahi',140000,'A')
,(5,'Martin',50000,'D'),(6,'Eric',80000,'C');</pre> </p>
<p><strong>One ALTER command to add multiple Indexes on Table:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_Employees 
ADD INDEX (EmpName), ADD INDEX (EmpGrade);</pre> </p>
<p><strong>One ALTER command to drop multiple Indexes on Table:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_Employees 
DROP INDEX EmpName, DROP INDEX EmpGrade;</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: ALTER TABLE to change Fillfactor Value</title>
		<link>https://www.dbrnd.com/2016/03/postgresql-alter-table-to-change-fillfactor-value/</link>
					<comments>https://www.dbrnd.com/2016/03/postgresql-alter-table-to-change-fillfactor-value/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 30 Mar 2016 04:30:54 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Fillfactor]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=2183</guid>

					<description><![CDATA[This is one of the important post related to change or set the Fillfactor value for the existing table of the PostgreSQL. One of our tables is very busy with a large number of updates and access by multiple clients. I also estimated the row size of that table which is not that much bigger, [&#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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&amp;linkname=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" 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%2F2016%2F03%2Fpostgresql-alter-table-to-change-fillfactor-value%2F&#038;title=PostgreSQL%3A%20ALTER%20TABLE%20to%20change%20Fillfactor%20Value" data-a2a-url="https://www.dbrnd.com/2016/03/postgresql-alter-table-to-change-fillfactor-value/" data-a2a-title="PostgreSQL: ALTER TABLE to change Fillfactor Value"></a></p><p>This is one of the important post related to change or set the Fillfactor value for the existing table of the PostgreSQL. </p>
<blockquote><p>One of our tables is very busy with a large number of updates and access by multiple clients.</p>
<p>I also estimated the row size of that table which is not that much bigger, so I decided to change the Fillfactor of that table. </p></blockquote>
<p><a href="https://www.dbrnd.com/2016/03/postgresql-the-awesome-table-fillfactor-to-speedup-update-and-select-statement/" target="_blank"><strong>I have recently published an article about the importance of Fillfactor in PostgreSQL. </strong></a></p>
<pre class="crayon-plain-tag">ALTER TABLE table_name SET ( fillfactor = 50);
VACUUM FULL table_name;</pre> </p>
<p><strong>Please Note:</strong></p>
<blockquote><p>After changing the value of Fillfactor, you should execute VACUUM FULL on that particular table.<br />
After changing the value of Fillfactor, it works only for new tuples.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/03/postgresql-alter-table-to-change-fillfactor-value/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<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>
