Postgresql – Why is pg_restore unable to create database with –create option before restoring from dump

linuxpg-restorepostgresqlrestore

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

Best Answer

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.