pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Andrus <kobruleht2@hot.ee>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: How to restore to empty database
Date: Thu, 30 Jan 2020 14:31:23 -0800
Message-ID: <8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com> (raw)
In-Reply-To: <D3F14323F35F4DC4BA3E517ED11199E1@dell2>
References: <D3F14323F35F4DC4BA3E517ED11199E1@dell2>

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





view thread (6+ messages)  latest in thread

Message-ID: <8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com>
Permalink:  ../8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com/
Also on:    postgresql.org/message-id/8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, kobruleht2@hot.ee
  Subject: Re: How to restore to empty database
  In-Reply-To: <8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com>
  References: <D3F14323F35F4DC4BA3E517ED11199E1@dell2> <8f1d29bd-907e-e1c5-cc5f-2b9cade5af19@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox