<?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>migrate schema data &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/migrate-schema-data/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, 13 Feb 2017 18:24:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Script to copy Table Data from one Schema to another Schema</title>
		<link>https://www.dbrnd.com/2017/02/postgresql-script-to-copy-schema-table-data-to-another-schema-table-migration/</link>
					<comments>https://www.dbrnd.com/2017/02/postgresql-script-to-copy-schema-table-data-to-another-schema-table-migration/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 08 Feb 2017 16:16:52 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[migrate schema data]]></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[schema]]></category>
		<category><![CDATA[schema copy]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4491</guid>

					<description><![CDATA[In this post, I am sharing one script to copy your Schema tables into another Schema of PostgreSQL. I found this script at wiki.postgresql.org and modified the coding standard for the best use. In our organization, I am creating a different schema for all the database developers, and at every new schema, we require to [&#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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" 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%2F02%2Fpostgresql-script-to-copy-schema-table-data-to-another-schema-table-migration%2F&#038;title=PostgreSQL%3A%20Script%20to%20copy%20Table%20Data%20from%20one%20Schema%20to%20another%20Schema" data-a2a-url="https://www.dbrnd.com/2017/02/postgresql-script-to-copy-schema-table-data-to-another-schema-table-migration/" data-a2a-title="PostgreSQL: Script to copy Table Data from one Schema to another Schema"></a></p><p>In this post, I am sharing one script to copy your Schema tables into another Schema of PostgreSQL.<br />
I found this script at wiki.postgresql.org and modified the coding standard for the best use. </p>
<p>In our organization, I am creating a different schema for all the database developers, and at every new schema, we require to migrate development origin schema&#8217;s table data into a newly created schema. </p>
<blockquote><p>I created different database users for each database developer, and they can only access their assigned schema. I am doing this for security reason, and it is also very easy to audit the changes and work progress of all the database developers. </p></blockquote>
<p>Create a below function and execute to migrate your schema.</p>
<p><strong>Create a function to copy old schema data to new schema:</strong></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_CopySchemaData(source_schema text, dest_schema text) 
RETURNS void AS
$$
 
DECLARE
  object text;
  buffer text;
  default_ text;
  column_ text;
BEGIN

	-- Create a new schema
	EXECUTE 'CREATE SCHEMA ' || dest_schema; 
  
FOR object IN
	SELECT sequence_name::text 
	FROM information_schema.SEQUENCES 
	WHERE sequence_schema = source_schema
LOOP
	EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || object;
END LOOP;
 
FOR object IN
	SELECT TABLE_NAME::text 
	FROM information_schema.TABLES 
	WHERE table_schema = source_schema
LOOP
	buffer := dest_schema || '.' || object;
	EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object || ' INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING DEFAULTS)';
 
	FOR column_, default_ IN
		SELECT column_name::text, REPLACE(column_default::text, source_schema, dest_schema) 
		FROM information_schema.COLUMNS 
		WHERE table_schema = dest_schema 
			AND TABLE_NAME = object 
			AND column_default LIKE 'nextval(%' || source_schema || '%::regclass)'
	LOOP
	EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFAULT ' || default_;
	END LOOP;
END LOOP;
 
END;
 
$$ LANGUAGE plpgsql VOLATILE;</pre> </p>
<p><strong>Sample Execution:</strong></p>
<pre class="crayon-plain-tag">select *from fn_CopySchemaData('old_schema','new_schema');</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/02/postgresql-script-to-copy-schema-table-data-to-another-schema-table-migration/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
