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

How do I use pg_dump and pg_restore to move a large database to another machine?

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

Problem

I'm a bit unclear on how to use the pg_dump/pg_restore duo to move a large-ish database.

Here is the command I use to dump the database (the host and username are taken from environmental variables):

pg_dump --dbname=mydb --format=custom > mydb.dump


I use the custom format because I do this over the network and want to use compression.

When I now try to restore it, I tried this:

pg_restore --dbname=mydb --no-owner mydb.dump


But I get:

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


I tried next with the --create option, but that yields the same error message.

Finally I tried passing template1 as a --dbname option:

pg_restore --dbname=template1 --no-owner --create mydb.dump


But here is what I get:

pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2125; 1262 16395 DATABASE mydb mydbuser
pg_restore: [archiver (db)] could not execute query: ERROR:  invalid locale name: "en_US.utf8"
    Command was: CREATE DATABASE mydb WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.utf8' LC_CTYPE = 'en_US.utf8';

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


What am I doing wrong here?

Solution

When you do

pg_restore --dbname=mydb --no-owner mydb.dump


You are trying to connect to the mydb database which still does not exist. Connect to an existent one and use the --create parameter:

pg_restore --dbname=existentdb --no-owner --create mydb.dump


http://www.postgresql.org/docs/current/static/app-pgrestore.html

Code Snippets

pg_restore --dbname=mydb --no-owner mydb.dump
pg_restore --dbname=existentdb --no-owner --create mydb.dump

Context

StackExchange Database Administrators Q#86386, answer score: 6

Revisions (0)

No revisions yet.