denishpatel / pg-clone-schema

Postgres clone schema utility without need of going outside of database. Makes developers life easy by running single function to clone schema with all objects. It is very handy on Postgres RDS. Utility is sponsored by http://elephas.io/
MIT License
173 stars 44 forks source link

clone_schema

clone_schema is a PostgreSQL utility that makes a copy of a given schema (DDL and/or DATA). It is based on the Community version of PostgreSQL. It works on most Linux distros and Windows versions. It also runs on PostgreSQL in the cloud (AWS, GCP, MS Azure).

Handles following objects:


Arguments:

source schema  Required: text - schema name
target schema  Required: text - table name
ENUM list      Required: One of 'DATA','NODATA','DDLONLY'
ENUM list      Optional: 'NOOWNER','NOACL','VERBOSE','FILECOPY'

By default, ownership and privileges are also cloned from source to target schema. To override, specify NOOWNER and/or NOACL (similar to how pg_dump works). When NOOWNER is specified, the one running the script is the default owner unless overridden by a SET ROLE command before running this script. You may get faster results copying data to/from disk instead of in-memory copy. FILECOPY is a workaround for tables with complex UDT-type columns that fail to copy. It only works for On-Prem PG Instances since it relies on using the COPY command to write to and read from disk.

Clone the schema with no data:

select clone_schema('sample', 'sample_clone', 'NODATA');

Clone the schema with data:

select clone_schema('sample', 'sample_clone', 'DATA');
select clone_schema('sample', 'sample_clone', 'DATA','VERBOSE'); -- show row copy progress

Just generate DDL:

select clone_schema('sample', 'sample_clone', 'DDLONLY');

In this case, standard output with "INFO" lines are the generated DDL.


The schema_object_counts.sql file is useful for validating the cloning results. Just run it against source and target schemas to validate object counts after changing default schema name, sample.

Regression Testing Overview

Regression Testing is done in the following order:

Assumptions

Limitations