Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pg_restore with -C option does not create the database

Tags:

I am using pg_dump and pg_restore for backup and restore of postgres database.

Here is some information from documentation that will be relevant for this question For Pg_restore, -C option is described as follows

-C

--create

Create the database before restoring into it. If --clean is also specified, > > drop and recreate the target database before connecting to it. 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.

However even when I use this option with pg_restore, I get following error

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

As per the description the -C option should have created the missing database. However it does not in my case.

Following are the steps that I did for backup and restore:

  1. Use pg_dump to backup database
pg_dump -N backup -d test --format custom -v -h xxhostxx -p 5432 -U xxuserxx --lock-wait-timeout 300000 -f test_pg_dump.dmp 

Note: not using -C option since it is meaningful for the plain-text formats only

  1. Deleted the test database

  2. use pg_resore to restore database

    pg_restore -C -d test -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp** 

I cannot understand what is the issue here! Am I doing anything wrong ? Let me know if more information is needed.

like image 555
Swapnil17 Avatar asked Nov 24 '16 11:11

Swapnil17


People also ask

Does Pg_restore create database?

It will NOT create that database. It creates a database with the name from the archive you are restoring and restores the data into that database.

What is Pg_restore in Postgres?

Description. pg_restore is a utility for restoring a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats. It will issue the commands necessary to reconstruct the database to the state it was in at the time it was saved.

How long does a Pg_restore take?

Its data is updated regularly as a result of tests, then a new dump must be made, and the dumps are regularly used to recreate the database in a well-defined state. I noted that the dump (using pg_dump -Fc database ) only takes a few seconds, but the restore ( pg_restore -d database ) takes about a minute.

Can I restore SQL database to PostgreSQL?

You can't restore it to a PostgreSQL database. If you need to migrate the data from SQL Server to Postgres you need to script the data out or use a Migration/ETL tool.


1 Answers

Exactly like @Eelke said - you've got in file wrote 'create database' so this database does not exist when you're running script... That's what for there is always 'postgres' database. Try this:

pg_restore -C -d postgres -v -h xxhostxx -p 5432 -U xxuserxx test_pg_dump.dmp** 

And this should:

  1. connect to postgres database
  2. Create test database
  3. Disconnect from postgres and connect to test
  4. Upload data into database

Of course check who is owner of postgres database - in most cases you have to run this as user 'postgres'.

like image 76
Jendrusk Avatar answered Sep 24 '22 12:09

Jendrusk