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

Why is pg_restore unable to create database with --create option before restoring from dump?

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

Problem

Why pg_restore is unable to create database with --create option before restoring from dump?

pg_restore --username=myuser --create --dbname=mydb --verbose  mybackup


Error,

pg_restore: [archiver (db)] connection to database "mydb" failed: FATAL:  database "mydb" does not exist

Solution

In order to create a database, it needs to be able to connect to your database server in order to issue the "CREATE DATABASE" command. Connecting to the server requires you to specify an existing database to connect under, and obviously that cannot be the database which fails to exist and which is to be created. There are special databases usually reserved for maintenance operations, usually postgres, which you can connect to for this purpose.

When using --create mode, --dbname specifies not the name of the database to be restored into, but rather the name of the database to be used to create the other database.

Context

StackExchange Database Administrators Q#185503, answer score: 2

Revisions (0)

No revisions yet.