Copying database objects from a local source to a target v16
There are two functions you can use with EDB Clone Schema to perform a local copy of a schema and its database objects:
localcopyschema
— This function copies a schema and its database objects from a source database into the same database (the target) but with a different schema name from the original. Use this function when the source schema and the copy will reside within the same database. See localcopyschema for more information.localcopyschema_nb
— This function performs the same purpose aslocalcopyschema
but as a background job, which frees up the terminal from which the function was initiated. This function is referred to as a non-blocking function. See localcopyschema_nb for more information.
Performing a local copy of a schema
The localcopyschema
function copies a schema and its database objects in a local database specified in the source_fdw
foreign server from the source schema to the specified target schema in the same database.
The function returns a Boolean value. If the function succeeds, then true
is returned. If the function fails, then false
is returned.
The source_fdw, source_schema, target_schema
, and log_filename
are required parameters while all other parameters are optional.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
source_schema
Name of the schema from which to clone database objects.
target_schema
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which information from the function is recorded. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
This example shows the cloning of schema edb
containing a set of database objects to target schema edbcopy
. Both schemas are in database edb
as defined by local_server
.
The example is for the following environment:
- Host on which the database server is running:
localhost
- Port of the database server:
5444
- Database source/target of the clone:
edb
- Foreign server (
local_server
) and user mapping with the information of the preceding bullet points - Source schema:
edb
- Target schema:
edbcopy
- Database superuser to invoke
localcopyschema: enterprisedb
Before invoking the function, database user enterprisedb
connects to to database edb
:
The following displays the logging status using the process_status_from_log
function:
Results
After the clone is complete, the following shows some of the database objects copied to the edbcopy
schema:
Performing a local copy of a schema as a batch job
The localcopyschema_nb
function copies a schema and its database objects in a local database specified in the source_fdw
foreign server from the source schema to the specified target schema in the same database. The copy occurs in a non-blocking manner as a job submitted to pgAgent.
The function returns an INTEGER
value job ID for the job submitted to pgAgent. If the function fails, then null is returned.
The source_fdw
, source
, target
, and log_filename
parameters are required. All other parameters are optional.
After the pgAgent job completes, remove it with the remove_log_file_and_job
function.
Parameters
source_fdw
Name of the foreign server managed by the postgres_fdw
foreign data wrapper from which to clone database objects.
source
Name of the schema from which to clone database objects.
target
Name of the schema into which to clone database objects from the source schema.
log_filename
Name of the log file in which to record information from the function. The log file is created under the directory specified by the log_directory
configuration parameter in the postgresql.conf
file.
on_tblspace
Boolean value to specify whether to create database objects in their tablespaces. If false
, then the TABLESPACE
clause isn't included in the applicable CREATE
DDL statement when added to the target schema. If true
, then the TABLESPACE
clause is included in the CREATE
DDL statement when added to the target schema. The default value is false
.
verbose_on
Boolean value to specify whether to print the DDLs in log_filename
when creating objects in the target schema. If false
, then DDLs aren't printed. If true
, then DDLs are printed. The default value is false
.
copy_acls
Boolean value to specify whether to include the access control list (ACL) while creating objects in the target schema. The access control list is the set of GRANT
privilege statements. If false
, then the access control list isn't included for the target schema. If true
, then the access control list is included for the target schema. The default value is false
.
worker_count
Number of background workers to perform the clone in parallel. The default value is 1
.
Example
The same cloning operation is performed as the example in localcopyschema
but using the non-blocking function localcopyschema_nb
.
You can use this command to see whether pgAgent is running on the appropriate local database:
If pgAgent isn't running, start it by executing the pgagent
option. The pgagent
program file is located in the bin
subdirectory of the EDB Postgres Advanced Server installation directory.
Note
The pgagent -l 2
option starts pgAgent in DEBUG
mode, which logs continuous debugging information into the log file specified with the -s
option. Use a lower value for the -l
option, or omit it entirely to record less information.
The localcopyschema_nb
function returns the job ID shown as 4
in the example.
The following displays the job status:
The following removes the pgAgent job: