Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aBPgN-00078s-D0 for pgsql-admin@arkaria.postgresql.org; Tue, 22 Dec 2015 16:21:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aBPgM-0005R6-Gx for pgsql-admin@arkaria.postgresql.org; Tue, 22 Dec 2015 16:21:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aBPeg-0003VL-SJ for pgsql-admin@postgresql.org; Tue, 22 Dec 2015 16:19:19 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aBPee-0006uU-41 for pgsql-admin@postgresql.org; Tue, 22 Dec 2015 16:19:17 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id F2495203A9 for ; Tue, 22 Dec 2015 11:19:14 -0500 (EST) Received: from web1 ([10.202.2.211]) by compute1.internal (MEProxy); Tue, 22 Dec 2015 11:19:14 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.fm; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=YovbPHC3+bafmszsD7o+fJNUUiw=; b=DjeOx0 qXndDkiwLTJu8N5ga7e4ju9S9iMoVOnlDVAcmfRMKmnt3z+tR4c3+sZhUENHcD+y eavy3n8u+SiuB2w+wADvPDBEmA2528XkQqr4nbAsSkbTowH3hDq+t5XqR97VbavU vpXPM00Eib2P3a/Niu3KQZcdxerb1hNewASvU= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=YovbPHC3+bafmsz sD7o+fJNUUiw=; b=qQ2kvY1+mEt3J0233aDNq1OUZGdqruLnIryJb6cDDQl5s4m yapka6/+ldJDio7sPMtgSCdPesL24cHBNxgOW6o8UOeLA3tQvXd1Sih75/VhRVXI NquWq0WNVBleWvTGR4GMjvyeHPhi5Lb+nb4YRZXNh+z9thSx/q1XSs5bHWew= Received: by web1.nyi.internal (Postfix, from userid 99) id BF169AEEDE1; Tue, 22 Dec 2015 11:19:14 -0500 (EST) Message-Id: <1450801154.79237.474120345.758EF3FA@webmail.messagingengine.com> X-Sasl-Enc: 7SA1gbN9fMNZlo5gFAgNTsGjyWxyA1m//38jvYrlwrDp 1450801154 From: CN To: pgsql-admin@postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: multipart/alternative; boundary="_----------=_1450801154792372"; charset="utf-8" X-Mailer: MessagingEngine.com Webmail Interface - ajax-a93c17cb In-Reply-To: <1450369108.2740801.470182945.59E4E975@webmail.messagingengine.com> References: <1450338245.2626908.469822713.6C14EE8F@webmail.messagingengine.com> <1450365586.2726209.470114761.326B7E39@webmail.messagingengine.com> <1450369108.2740801.470182945.59E4E975@webmail.messagingengine.com> Subject: Re: feature request: pg_restore renaming target schema Date: Wed, 23 Dec 2015 00:19:14 +0800 X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org This is a multi-part message in MIME format. --_----------=_1450801154792372 Content-Transfer-Encoding: 7bit Content-Type: text/plain 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 --_----------=_1450801154792372 Content-Transfer-Encoding: 7bit Content-Type: text/html
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
--_----------=_1450801154792372--