Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
87 views

Script To Copy Table Data From One Schema To Another Schema

This script copies table data from one PostgreSQL schema to another. It creates a function that loops through the tables and sequences in the source schema and copies them to the destination schema, including constraints, indexes, and default values. The function also updates any default values that refer to the source schema in the new tables. Sample usage of the function to copy data from the "old_schema" to the "new_schema" is provided.

Uploaded by

leonard1971
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Script To Copy Table Data From One Schema To Another Schema

This script copies table data from one PostgreSQL schema to another. It creates a function that loops through the tables and sequences in the source schema and copies them to the destination schema, including constraints, indexes, and default values. The function also updates any default values that refer to the source schema in the new tables. Sample usage of the function to copy data from the "old_schema" to the "new_schema" is provided.

Uploaded by

leonard1971
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PostgreSQL: Script to copy Table

Data from one Schema to another


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

Facebookgoogle_plusTwitterLinkedInPinterestRedd
itPocketShare
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 migrate development origin schema’s table
data into a newly created schema.

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.
Create a below function and execute to migrate your schema.
Create a function to copy old schema data to new schema:
1 CREATE OR REPLACE FUNCTION fn_CopySchemaData(source_schema text, dest_schema te
2 xt)
3 RETURNS void AS
4 $$
5
6 DECLARE
7 object text;
8 buffer text;
9 default_ text;
10 column_ text;
11 BEGIN
12
13 -- Create a new schema
14 EXECUTE 'CREATE SCHEMA ' || dest_schema;
15
16 FOR object IN
17 SELECT sequence_name::text
18 FROM information_schema.SEQUENCES
19 WHERE sequence_schema = source_schema
20 LOOP
21 EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || object;
22 END LOOP;
23
24 FOR object IN
25 SELECT TABLE_NAME::text
26 FROM information_schema.TABLES
27 WHERE table_schema = source_schema
28 LOOP
29 buffer := dest_schema || '.' || object;
30 EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object || ' INC
31 LUDING CONSTRAINTS INCLUDING INDEXES INCLUDING DEFAULTS)';
32
33 FOR column_, default_ IN
34 SELECT column_name::text, REPLACE(column_default::text, source_schema,
35 dest_schema)
36 FROM information_schema.COLUMNS
37 WHERE table_schema = dest_schema
38 AND TABLE_NAME = object
39 AND column_default LIKE 'nextval(%' || source_schema || '%::regclass
40 )'
41 LOOP
42 EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFA
43 ULT ' || default_;
44 END LOOP;
END LOOP;

END;

$$ LANGUAGE plpgsql VOLATILE;


Sample Execution:
1 select *from fn_CopySchemaData('old_schema','new_schema');
Please visit other related articles...
 PostgreSQL 9.4: Using FILTER CLAUSE, multiple COUNT(*) in one SELECT Query for Different Groups

 PostgreSQL: Move Table with Data from one Schema to another Schema

 PostgreSQL: Shell script to copy table data from one server to another server

 PostgreSQL: Script to search any Text from the Stored Function

 PostgreSQL: Script to find all Objects of a Particular User

 PostgreSQL: Find a list of active Temp tables with Size and User information

 PostgreSQL: Script to find the count of objects for each Database Schema

 PostgreSQL: Script to convert User to Super User

You might also like