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 1ixIDo-000444-VT for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 22:23:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ixIDn-0005WD-CG for pgsql-general@arkaria.postgresql.org; Thu, 30 Jan 2020 22:23:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixIDn-0005W6-2k for pgsql-general@lists.postgresql.org; Thu, 30 Jan 2020 22:23:35 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ixIDg-0008Vw-F3 for pgsql-general@postgresql.org; Thu, 30 Jan 2020 22:23:34 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 2C52D18AA for ; Fri, 31 Jan 2020 00:23:27 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id J59G7n6E5Mh6 for ; Fri, 31 Jan 2020 00:23:25 +0200 (EET) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id F3F2C1347 for ; Fri, 31 Jan 2020 00:23:24 +0200 (EET) Received: from dell2 (66-63-196-88.sta.estpak.ee [88.196.63.66]) (Authenticated sender: kobruleht2) by HOT-Relayhost1.estpak.ee (Postfix) with ESMTPSA id 487vy46ffwz8sX for ; Fri, 31 Jan 2020 00:23:23 +0200 (EET) Message-ID: From: "Andrus" To: "pgsql-general" Subject: How to restore to empty database Date: Fri, 31 Jan 2020 00:23:15 +0200 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="windows-1257"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Mailer: Microsoft Windows Live Mail 16.4.3528.331 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3528.331 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 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.