Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixILS-0004H2-GS for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 22:31:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ixILQ-0002W9-5x for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 22:31:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixILP-0002Vu-Oi for pgsql-general@lists.postgresql.org; Thu, 30 Jan 2020 22:31:27 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ixILN-0002Uz-Jw for pgsql-general@postgresql.org; Thu, 30 Jan 2020 22:31:26 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 7EAD321910; Thu, 30 Jan 2020 17:31:24 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Thu, 30 Jan 2020 17:31:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=q TP7NxLv1TWTGxxpIFjnwqqZ+6UEECcc9u1qxCilwb8=; b=aa2/vf5pT/Az9WyWR Avh4UePa/gTuTV3BESIGOMsuJ3dEO1NnAbgKi5SGXLuz0xMpfF6AWCpOvzQRGg+s 7Mu9vh/t7TA4PDuVt925djiK2sQwaXut/ekIvg2eewJ1VOFHCyrAXfKZojucdsif uFx/A2gAwX+N+OWn1vmKaQOcUAmgTJMZye9Rq7m4kEH0Rtt3oYC0ZQ45FZJkJnKj JoWtnwFMrGsyVldFDCPcIFJtJX5CVN+RhpSTfbr/VCOQmodCLAC9AyEv6B8+tR9c uAyRei3bXQzJ3eDTjvZUHf+QdwimaC7DTqtjazisZlSbPfTJjnxZ6trUDifnE8YM +7vUg== DKIM-Signature: v=1; a=rsa-sha256; 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-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=qTP7NxLv1TWTGxxpIFjnwqqZ+6UEECcc9u1qxCilw b8=; b=moF8oFqB+FhGwmQt2k1aVUKFRfgTNe8CuKdEIRgM6qiPVpiyOY3YosFHO Bk05iBeHv1UNU8bcqisM1+bSwEdhATCCHS1tVTu7Gjt4U+nHrMCIjR3Ihfr1FPOd CKo2AxiHItQ60zuGFyWeMys7MTMhU0eEYixWX7klBhliHyA/vJ57eK4Z8IBgLQwp rt4Bo13Lcyb3wOFarvbCimdgzBysAJjuF+cxnO50Jlp5nzGO39FNx2GF2lMXAnxe itrF3Sh+3yijh+NcGcqe1N3f7vIR7mZraynnw/A6C2lPS8tXCGgouYwLDOFxV2Md vQrkTpB10+sjTJFxwYRpKkJqSPytA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrfeekgdduheelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfehhnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddruddvledrud elfeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id D6B823280062; Thu, 30 Jan 2020 17:31:23 -0500 (EST) Subject: Re: How to restore to empty database To: Andrus , pgsql-general References: From: Adrian Klaver Message-ID: <8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com> Date: Thu, 30 Jan 2020 14:31:23 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1257; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/30/20 2:23 PM, Andrus wrote: > Hi! > > I want to restore to new empty database using pg_restore . > pg_restore should create new empty database and restore into it. > If database exists, pg_restore should clean all data from it or drop and > create new empty database before restore. > > According to pg_restore doc, switches --clean --create --if-exists > should do this. > > I tried > >    PG_COLOR=auto >    PGHOST=localhost >    PGPASSWORD=mypass >    PGUSER=postgres >    export PGHOST  PGPASSWORD PG_COLOR PGUSER >    pg_restore --clean --create --if-exists --dbname=mydb  --jobs=4 > --verbose "mydb.backup" > > but got error > > pg_restore: connecting to database for restore > pg_restore: error: connection to database "mydb" failed: FATAL: > database "mydb" does not exist > > I also tried without --dbname=mydb but then got error > > pg_restore: error: one of -d/--dbname and -f/--file must be specified You need to connect to a database that exists with --dbname, for instance --dbname=postgres. Postgres will then use that connection to create the new database, in your case mydb. > > How to restore to database which does not exist to drop existing > database before restore if it exists ? > Should I invoke > > drop database if exists > > and > > create database > > commands before calling pg_restore or can pg_restore do it itself ? > > Using Postgres 12 on Debian 10 > > Andrus. > > -- Adrian Klaver adrian.klaver@aklaver.com