Postgresql – pg_restore to create database before loading it

postgresql

I understand that pg_restore needs to connect to a database before it can start the restore process, is there anyway pg_restore can use the "postgres" database to create another database?

I have the following command

pg_restore -h "192.168.1.212" -p 5432 -U mysuperusername -C -d bp bp.custom

when I run the command I get the following message

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

Is there anyway for pg_restore to create the "bp" database without me having to create it manually?

Best Answer

Specify -d postgres on the command line.

Quote from the manual for the -C option. The database to be restored is automatically take from the backup file

When this option is used, the database named with -d is used only to issue the initial DROP DATABASE and CREATE DATABASE commands. All data is restored into the database name that appears in the archive.

pg_restore -h "192.168.1.212" -p 5432 -U mysuperusername -C -d postgres bp.custom