HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

When pg_restore honours --dbname option

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
dbnamehonoursoptionwhenpg_restore

Problem

I am trying to restore a binary pg_dump backup under a different database name that was in the original dump. Despite giving --dbname command line argument, pg_restore tries to restore everything under the original database name - the behavior I especially want to avoid.

createdb mydb

pg_restore --dbname=mydb --exit-on-error -Fc database.bak

pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 8; 2615 16396 SCHEMA topology u9o2f4j0
pg_restore: [archiver (db)] could not execute query: ERROR: schema "topology" already exists
Command was: CREATE SCHEMA topology;


How can I make pg_restore to restore the database under a different name?

pg_dump --version
pg_dump (PostgreSQL) 9.5.5

pg_restore --version
pg_restore (PostgreSQL) 9.6.1

Solution

There are few command line options you need to consider.

pg_restore -x --no-owner --dbname=mynewdb --exit-on-error -Fc database.bak


-
-Fc: restore from a binary back up as its more flexible than plain text SQL dump

-
--dbname: restore under this database name (must be created beforehand using createdb)

-
-x: do not try to restore access priviledges (e.g. the superuser name may change between databases)

-
--no-owner: the database owner user is different on the system where restore is performed

-
--exit-on-error: If bad stuff happens, abort and don't flood your terminal with useless errors

Context

StackExchange Database Administrators Q#162157, answer score: 5

Revisions (0)

No revisions yet.