Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just restore to a different database. For <code>pg_restore</code> of <code>-Fc</code> dumps from <code>pg_dump</code>'s custom format:</p> <pre><code>createdb newdbname pg_restore --dbname newdbname database.dump </code></pre> <p>For SQL-format dumps <em>not</em> created with the <code>-C</code> option to <code>pg_dump</code>:</p> <pre><code>createdb newdbname psql -f database_dump.sql newdbname </code></pre> <p>If you're streaming the dump from a remote host, just omit <code>-f database_dump.sql</code> as the dump data is coming from stdin.</p> <p>You can't easily <code>CREATE DATABASE</code> in the same command as your restore, because you need to connect to a different DB like <code>template1</code> in order to create the new DB. So in your example you might:</p> <pre><code>psql -h localhost -U localadmin template1 -c 'CREATE DATABASE newdb;' pg_dump -U remoteuser -h remoteServer dbname | psql -h localhost -U localadmin newdb </code></pre> <p>Note the omission of the <code>-C</code> flag to <code>pg_dump</code>.</p> <p>The first command is just the longhand way of writing <code>createdb -h localhost -U localadmin newdb</code>.</p> <hr> <p><strong>Update</strong>: If you're stuck with a <code>pg_dump</code> created with the <code>-C</code> flag you can indeed just <code>sed</code> the 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's <code>pg_dump -C</code>:</p> <pre><code>-- -- Name: regress; Type: DATABASE; Schema: -; Owner: craig -- CREATE DATABASE regress WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8'; ALTER DATABASE regress OWNER TO craig; \connect regress </code></pre> <p>This can be transformed quite safely with three (or four if you want to rewrite the comment) very specific <code>sed</code> commands. Do <em>not</em> just do a global find and replace on the database name, though.</p> <pre><code>sed \ -e 's/^CREATE DATABASE regress/CREATE DATABASE newdbname/' \ -e 's/^ALTER DATABASE regress/ALTER DATABASE newdbname/' \ -e 's/^\\connect regress/\\connect newdbname/' \ -e 's/^--Name: regress/--Name: newdbname/' </code></pre> <p>This should be a last resort; it's much better to just dump without <code>-C</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload