I am trying to setup a script to take a copy of a database from one server to another.
Thanks to this post Copying PostgreSQL database to another server I have found a way to do that.
But what I need to do is change the name of the database during the copy.
I have thought about using sed and doing a simple text replace. But I am worried that this could corrupt the database.
Does any one know the proper way of doing this?
As requested here are the commands I am using
pg_dump -C -U remoteuser -h remoteServer dbname | psql -h localhost -U localadmin template1
Just restore to a different database. For
pg_restoreof-Fcdumps frompg_dump‘s custom format:For SQL-format dumps not created with the
-Coption topg_dump:If you’re streaming the dump from a remote host, just omit
-f database_dump.sqlas the dump data is coming from stdin.You can’t easily
CREATE DATABASEin the same command as your restore, because you need to connect to a different DB liketemplate1in order to create the new DB. So in your example you might:Note the omission of the
-Cflag topg_dump.The first command is just the longhand way of writing
createdb -h localhost -U localadmin newdb.Update: If you’re stuck with a
pg_dumpcreated with the-Cflag you can indeed justsedthe dump so long as you’re extremely careful. There should only be four lines (one a comment) at the start of the file that refer to the database name. For the database name “regress” dumped with Pg 9.1’spg_dump -C:This can be transformed quite safely with three (or four if you want to rewrite the comment) very specific
sedcommands. Do not just do a global find and replace on the database name, though.This should be a last resort; it’s much better to just dump without
-C.