Re: feature request: pg_restore renaming target schema - Mailing list pgsql-admin

From CN
Subject Re: feature request: pg_restore renaming target schema
Date
Msg-id 1450801154.79237.474120345.758EF3FA@webmail.messagingengine.com
Whole thread Raw
In response to Re: feature request: pg_restore renaming target schema  (CN <cnliou9@fastmail.fm>)
List pgsql-admin
I am pleased to revoke this request.
 
Once option -O is applied to pg_dump, only two lines in the dumped file need to be edited.
 
In case it is also useful to anyone, here is the script I use to dump schema "ss" from source database "sd" and restore as new schema "ts" to database "td". Hard disk is not used as intermediate file is not created. I have a feeling that it is safe to run this script concurrently.
 
#!/bin/sh
SOURCE_DATABASE=sd
TARGET_DATABASE=td
TEMPLATE_SCHEMA=ss
NEW_SCHEMA=ts
pg_dump -O -E utf8 -n $TEMPLATE_SCHEMA -w -U postgres $SOURCE_DATABASE 2>&1 |
awk -v source="$TEMPLATE_SCHEMA" -v target="$NEW_SCHEMA" '
BEGIN {
pattern1="CREATE SCHEMA " source ";";
pattern2="SET search_path = " source ", pg_catalog;";
pattern1_is_done=0
pattern2_is_done=0
is_data=0;
print "BEGIN;"; #Wrap all statements in single transaction.
}
{
if(!is_data){
if(substr($0,1,2) == "--")
$0="";
else if(!pattern1_is_done){
if($0 == pattern1){
$0="CREATE SCHEMA " target ";";
pattern1_is_done=1;
}
}
else if(!pattern2_is_done){
if($0 == pattern2){
$0="SET search_path TO " target ";";
pattern2_is_done=1;
}
}
else if(match($0,/^COPY .+ FROM stdin;$/) > 0)
is_data=1;
}
print
}
END{
print "COMMIT;"; #Wrap all statements in single transaction.
}
' |
psql -q -o /dev/null -w -U postgres $TARGET_DATABASE 2>&1
vacuumdb -q -Z -w -U postgres $TARGET_DATABASE 2>&1

 
-- 
http://www.fastmail.com - The professional email service

pgsql-admin by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: [PERFORM] Connections "Startup"
Next
From: Jim Nasby
Date:
Subject: Re: [PERFORM] Connections "Startup"