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 1hEisH-00063D-QL for pgsql-admin@arkaria.postgresql.org; Thu, 11 Apr 2019 23:12:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hEisE-0005uR-GU for pgsql-admin@arkaria.postgresql.org; Thu, 11 Apr 2019 23:12:50 +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 1hEisD-0005jV-VD for pgsql-admin@lists.postgresql.org; Thu, 11 Apr 2019 23:12:50 +0000 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEisB-0006i4-3v for pgsql-admin@lists.postgresql.org; Thu, 11 Apr 2019 23:12:48 +0000 Received: by mail-ot1-x343.google.com with SMTP id t8so6773968otp.7 for ; Thu, 11 Apr 2019 16:12:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=suQyRPKZkYgi1rg3asKIHvgGCM2P0Is2+GTd92aC7Fw=; b=HFhDGOoQefZUFc2uPrsKFelUEA1erLMdHf0cO8lPczh9Yj1IczqbmhhwqxvX7Ojv7Y g+ksD6LWrAW46LkDvyrAKc4MmzJtg9ysaJCxU4Kd4/Ejj1ohX0lpH+zYoPhpcYgqvvOd AK0w0JoKJK3wmK85BAcvTJMhsM0IIdvf+S3Ki9dGHksOoVWbSsjIsMpeDNYJiB8HwlT9 bRCzFJD6rowmS5TA9euVGhPb+bnnAagFgB6rzKCZLUTpzMieKOJqu78/FOo59Cta/Ojs rT86eOcrARjhO/YfvvPXcVyCOuFSZN2Lv+Dj/KK/T+qptj+xx1T39DxvT/wQFTQHHYHT yHiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=suQyRPKZkYgi1rg3asKIHvgGCM2P0Is2+GTd92aC7Fw=; b=JGT5WI4jkzJUNClYPNJdwomylwK6WLa5EM3EnpJiTP/9xb/1EdpK0y+cT6MdNYxYvX K5raIggMm0pM/MgFJVxIF4wj8bMsyS6FR/GqnUOFQHUFJe17U/Wk03A7yggtmRwySN3Q 7V3j2DaMYfzM6uj/Vz11MQI8sjcg/cCIO17VhdlHP2U7gG/ilgVYpk5WyYtD6JFkN6Su NrzYGWc7NIL2hzZUK4YEO08KleT/0QvXmfzTd3ivdLJpsIAj3SFnfm+exx7tgcnbVAVJ 0fs9OLfxLXo47SItiIPIeVBqNR5r7jFN1/sdh9G8uSIyjnRyQte7VwirXx15XICvCr6C o4MA== X-Gm-Message-State: APjAAAXzmf8f97YCWAfKxbye8e/GL9SNI1X5QhPL3GIY3WNLgT4VoZIS 6D759MCw+38gugz61OFkaP0nJWJBYkk= X-Google-Smtp-Source: APXvYqzP1xRQAdU8quXVoPb/Gdsqs3vsGJgqmkR95cG+RgBLdwtL7C1v5/zWfHrgQL1HeOiQRIr1dA== X-Received: by 2002:a05:6830:183:: with SMTP id q3mr34176872ota.204.1555024366042; Thu, 11 Apr 2019 16:12:46 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id s63sm13299466oif.52.2019.04.11.16.12.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Apr 2019 16:12:45 -0700 (PDT) Subject: Re: pg_restore To: pgsql-admin@lists.postgresql.org References: From: Ron Message-ID: Date: Thu, 11 Apr 2019 18:12:44 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------63354FF5F29AAB86E9D7F5E1" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------63354FF5F29AAB86E9D7F5E1 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 4/11/19 5:09 PM, rammohan ganapavarapu wrote: > Hi, > > I have a custome database called mudb along with default databases in my > postgresql cluster, i am trying to restore mydb from a custom format > pg_dump file, what is the procedure to do it? > > psql -t -h localhost  -U ${RUN_USER}  -c "SELECT > pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE > pg_stat_activity.datname = 'mydb' AND pid <> pg_backend_pid();" > > pg_restore" -h localhost  -U ${RUN_USER}  -d mydb -j "${JOBS}" -x -O -C -c > --if-exist -v pgdump.dump > > But i am getting below error: > > pg_restore: connecting to database for restore > pg_restore: dropping DATABASE mydb > pg_restore: [archiver (db)] Error while PROCESSING TOC: > pg_restore: [archiver (db)] Error from TOC entry 4312; 1262 16384 DATABASE > mydb mydb > pg_restore: [archiver (db)] could not execute query: ERROR:  cannot drop > the currently open database >     Command was: DROP DATABASE IF EXISTS mydb; > > So what is the best way to restore? I am using 9.6 version. I made the same mistake you did, by doing "-d mydb".  You need to specify the postgres database. This works for me: export PGUSER=${RUN_USER} export PGHOST=localhost DB=mydb pg_restore -xOvcC --if-exists --jobs=${JOBS} *-d postgres*${DB} 2> ${DB}_restore.log -- Angular momentum makes the world go 'round. --------------63354FF5F29AAB86E9D7F5E1 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 4/11/19 5:09 PM, rammohan ganapavarapu wrote:
Hi,

I have a custome database called mudb along with default databases in my postgresql cluster, i am trying to restore mydb from a custom format pg_dump file, what is the procedure to do it?

psql -t -h localhost  -U ${RUN_USER}  -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'mydb' AND pid <> pg_backend_pid();"

pg_restore" -h localhost  -U ${RUN_USER}  -d mydb -j "${JOBS}" -x -O -C -c --if-exist -v pgdump.dump 

But i am getting below error:

pg_restore: connecting to database for restore
pg_restore: dropping DATABASE mydb
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4312; 1262 16384 DATABASE mydb mydb
pg_restore: [archiver (db)] could not execute query: ERROR:  cannot drop the currently open database
    Command was: DROP DATABASE IF EXISTS mydb;

So what is the best way to restore? I am using 9.6 version.

I made the same mistake you did, by doing "-d mydb".  You need to specify the postgres database.

This works for me:
export PGUSER=${RUN_USER}
export PGHOST=localhost
DB=mydb
pg_restore -xOvcC --if-exists --jobs=${JOBS} -d postgres ${DB} 2> ${DB}_restore.log


--
Angular momentum makes the world go 'round.
--------------63354FF5F29AAB86E9D7F5E1--