~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/pg_restore.sgml

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!-- doc/src/sgml/ref/pg_restore.sgml -->
 
2
 
 
3
<refentry id="APP-PGRESTORE">
 
4
 <refmeta>
 
5
  <refentrytitle>pg_restore</refentrytitle>
 
6
  <manvolnum>1</manvolnum>
 
7
  <refmiscinfo>Application</refmiscinfo>
 
8
 </refmeta>
 
9
 
 
10
 <refnamediv>
 
11
  <refname>pg_restore</refname>
 
12
 
 
13
  <refpurpose>
 
14
   restore a <productname>PostgreSQL</productname> database from an
 
15
   archive file created by <application>pg_dump</application>
 
16
  </refpurpose>
 
17
 </refnamediv>
 
18
 
 
19
 <indexterm zone="app-pgrestore">
 
20
  <primary>pg_restore</primary>
 
21
 </indexterm>
 
22
 
 
23
 <refsynopsisdiv>
 
24
  <cmdsynopsis>
 
25
   <command>pg_restore</command>
 
26
   <arg rep="repeat"><replaceable>connection-option</replaceable></arg>
 
27
   <arg rep="repeat"><replaceable>option</replaceable></arg>
 
28
   <arg><replaceable>filename</replaceable></arg>
 
29
  </cmdsynopsis>
 
30
 </refsynopsisdiv>
 
31
 
 
32
 
 
33
 <refsect1 id="app-pgrestore-description">
 
34
  <title>Description</title>
 
35
 
 
36
  <para>
 
37
   <application>pg_restore</application> is a utility for restoring a
 
38
   <productname>PostgreSQL</productname> database from an archive
 
39
   created by <xref linkend="app-pgdump"> in one of the non-plain-text
 
40
   formats.  It will issue the commands necessary to reconstruct the
 
41
   database to the state it was in at the time it was saved.  The
 
42
   archive files also allow <application>pg_restore</application> to
 
43
   be selective about what is restored, or even to reorder the items
 
44
   prior to being restored. The archive files are designed to be
 
45
   portable across architectures.
 
46
  </para>
 
47
 
 
48
  <para>
 
49
   <application>pg_restore</application> can operate in two modes.
 
50
   If a database name is specified, <application>pg_restore</application>
 
51
   connects to that database and restores archive contents directly into
 
52
   the database.  Otherwise, a script containing the SQL
 
53
   commands necessary to rebuild the database is created and written
 
54
   to a file or standard output.  This script output is equivalent to
 
55
   the plain text output format of <application>pg_dump</application>.
 
56
   Some of the options controlling the output are therefore analogous to
 
57
   <application>pg_dump</application> options.
 
58
  </para>
 
59
 
 
60
  <para>
 
61
   Obviously, <application>pg_restore</application> cannot restore information
 
62
   that is not present in the archive file.  For instance, if the
 
63
   archive was made using the <quote>dump data as
 
64
   <command>INSERT</command> commands</quote> option,
 
65
   <application>pg_restore</application> will not be able to load the data
 
66
   using <command>COPY</command> statements.
 
67
  </para>
 
68
 </refsect1>
 
69
 
 
70
 <refsect1 id="app-pgrestore-options">
 
71
  <title>Options</title>
 
72
 
 
73
   <para>
 
74
    <application>pg_restore</application> accepts the following command
 
75
    line arguments.
 
76
 
 
77
    <variablelist>
 
78
     <varlistentry>
 
79
      <term><replaceable class="parameter">filename</replaceable></term>
 
80
      <listitem>
 
81
       <para>
 
82
       Specifies the location of the archive file (or directory, for a
 
83
       directory-format archive) to be restored.
 
84
       If not specified, the standard input is used.
 
85
       </para>
 
86
      </listitem>
 
87
     </varlistentry>
 
88
 
 
89
     <varlistentry>
 
90
      <term><option>-a</option></term>
 
91
      <term><option>--data-only</option></term>
 
92
      <listitem>
 
93
       <para>
 
94
        Restore only the data, not the schema (data definitions).
 
95
       </para>
 
96
      </listitem>
 
97
     </varlistentry>
 
98
 
 
99
     <varlistentry>
 
100
      <term><option>-c</option></term>
 
101
      <term><option>--clean</option></term>
 
102
      <listitem>
 
103
       <para>
 
104
        Clean (drop) database objects before recreating them.
 
105
       </para>
 
106
      </listitem>
 
107
     </varlistentry>
 
108
 
 
109
     <varlistentry>
 
110
      <term><option>-C</option></term>
 
111
      <term><option>--create</option></term>
 
112
      <listitem>
 
113
       <para>
 
114
        Create the database before restoring into it.  (When this
 
115
        option is used, the database named with <option>-d</option> is
 
116
        used only to issue the initial <command>CREATE DATABASE</>
 
117
        command.  All data is restored into the database name that
 
118
        appears in the archive.)
 
119
       </para>
 
120
      </listitem>
 
121
     </varlistentry>
 
122
 
 
123
     <varlistentry>
 
124
      <term><option>-d <replaceable class="parameter">dbname</replaceable></option></term>
 
125
      <term><option>--dbname=<replaceable class="parameter">dbname</replaceable></option></term>
 
126
      <listitem>
 
127
       <para>
 
128
        Connect to database <replaceable
 
129
        class="parameter">dbname</replaceable> and restore directly
 
130
        into the database.
 
131
       </para>
 
132
      </listitem>
 
133
     </varlistentry>
 
134
 
 
135
     <varlistentry>
 
136
      <term><option>-e</option></term>
 
137
      <term><option>--exit-on-error</option></term>
 
138
      <listitem>
 
139
       <para>
 
140
        Exit if an error is encountered while sending SQL commands to
 
141
        the database. The default is to continue and to display a count of
 
142
        errors at the end of the restoration.
 
143
       </para>
 
144
      </listitem>
 
145
     </varlistentry>
 
146
 
 
147
     <varlistentry>
 
148
      <term><option>-f <replaceable>filename</replaceable></option></term>
 
149
      <term><option>--file=<replaceable>filename</replaceable></option></term>
 
150
      <listitem>
 
151
       <para>
 
152
        Specify output file for generated script, or for the listing
 
153
        when used with <option>-l</option>. Default is the standard
 
154
        output.
 
155
       </para>
 
156
      </listitem>
 
157
     </varlistentry>
 
158
 
 
159
     <varlistentry>
 
160
      <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
 
161
      <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
 
162
      <listitem>
 
163
       <para>
 
164
        Specify format of the archive.  It is not necessary to specify
 
165
        the format, since <application>pg_restore</application> will
 
166
        determine the format automatically. If specified, it can be
 
167
        one of the following:
 
168
 
 
169
       <variablelist>
 
170
        <varlistentry>
 
171
         <term><literal>d</></term>
 
172
         <term><literal>directory</></term>
 
173
         <listitem>
 
174
          <para>
 
175
           The archive is a <command>directory</command> archive.
 
176
          </para>
 
177
         </listitem>
 
178
        </varlistentry>
 
179
 
 
180
        <varlistentry>
 
181
         <term><literal>t</></term>
 
182
         <term><literal>tar</></term>
 
183
         <listitem>
 
184
          <para>
 
185
           The archive is a <command>tar</command> archive.
 
186
          </para>
 
187
         </listitem>
 
188
        </varlistentry>
 
189
 
 
190
        <varlistentry>
 
191
         <term><literal>c</></term>
 
192
         <term><literal>custom</></term>
 
193
         <listitem>
 
194
          <para>
 
195
           The archive is in the custom format of
 
196
           <application>pg_dump</application>.
 
197
          </para>
 
198
         </listitem>
 
199
        </varlistentry>
 
200
       </variablelist>
 
201
       </para>
 
202
      </listitem>
 
203
     </varlistentry>
 
204
 
 
205
     <varlistentry>
 
206
      <term><option>-i</option></term>
 
207
      <term><option>--ignore-version</option></term>
 
208
      <listitem>
 
209
       <para>
 
210
        A deprecated option that is now ignored.
 
211
       </para>
 
212
      </listitem>
 
213
     </varlistentry>
 
214
 
 
215
     <varlistentry>
 
216
      <term><option>-I <replaceable class="parameter">index</replaceable></option></term>
 
217
      <term><option>--index=<replaceable class="parameter">index</replaceable></option></term>
 
218
      <listitem>
 
219
       <para>
 
220
        Restore definition of named index only.
 
221
       </para>
 
222
      </listitem>
 
223
     </varlistentry>
 
224
 
 
225
     <varlistentry>
 
226
      <term><option>-j <replaceable class="parameter">number-of-jobs</replaceable></option></term>
 
227
      <term><option>--jobs=<replaceable class="parameter">number-of-jobs</replaceable></option></term>
 
228
      <listitem>
 
229
       <para>
 
230
        Run the most time-consuming parts
 
231
        of <application>pg_restore</> &mdash; those which load data,
 
232
        create indexes, or create constraints &mdash; using multiple
 
233
        concurrent jobs.  This option can dramatically reduce the time
 
234
        to restore a large database to a server running on a
 
235
        multiprocessor machine.
 
236
       </para>
 
237
 
 
238
       <para>
 
239
        Each job is one process or one thread, depending on the
 
240
        operating system, and uses a separate connection to the
 
241
        server.
 
242
       </para>
 
243
 
 
244
       <para>
 
245
        The optimal value for this option depends on the hardware
 
246
        setup of the server, of the client, and of the network.
 
247
        Factors include the number of CPU cores and the disk setup.  A
 
248
        good place to start is the number of CPU cores on the server,
 
249
        but values larger than that can also lead to faster restore
 
250
        times in many cases.  Of course, values that are too high will
 
251
        lead to decreased performance because of thrashing.
 
252
       </para>
 
253
 
 
254
       <para>
 
255
        Only the custom archive format is supported with this option.
 
256
        The input file must be a regular file (not, for example, a
 
257
        pipe).  This option is ignored when emitting a script rather
 
258
        than connecting directly to a database server.  Also, multiple
 
259
        jobs cannot be used together with the
 
260
        option <option>--single-transaction</option>.
 
261
       </para>
 
262
      </listitem>
 
263
     </varlistentry>
 
264
 
 
265
     <varlistentry>
 
266
      <term><option>-l</option></term>
 
267
      <term><option>--list</option></term>
 
268
      <listitem>
 
269
       <para>
 
270
        List the contents of the archive. The output of this operation
 
271
        can be used as input to the <option>-L</option> option.  Note that
 
272
        if filtering switches such as <option>-n</> or <option>-t</> are
 
273
        used with <option>-l</>, they will restrict the items listed.
 
274
       </para>
 
275
      </listitem>
 
276
     </varlistentry>
 
277
 
 
278
     <varlistentry>
 
279
      <term><option>-L <replaceable class="parameter">list-file</replaceable></option></term>
 
280
      <term><option>--use-list=<replaceable class="parameter">list-file</replaceable></option></term>
 
281
      <listitem>
 
282
       <para>
 
283
        Restore only those archive elements that are listed in <replaceable
 
284
        class="PARAMETER">list-file</replaceable>, and restore them in the
 
285
        order they appear in the file.  Note that
 
286
        if filtering switches such as <option>-n</> or <option>-t</> are
 
287
        used with <option>-L</>, they will further restrict the items restored.
 
288
       </para>
 
289
       <para>
 
290
        <replaceable class="PARAMETER">list-file</> is normally created by
 
291
        editing the output of a previous <option>-l</> operation.
 
292
        Lines can be moved or removed, and can also
 
293
        be commented out by placing a semicolon (<literal>;</literal>) at the
 
294
        start of the line.  See below for examples.
 
295
       </para>
 
296
      </listitem>
 
297
     </varlistentry>
 
298
 
 
299
     <varlistentry>
 
300
      <term><option>-n <replaceable class="parameter">namespace</replaceable></option></term>
 
301
      <term><option>--schema=<replaceable class="parameter">schema</replaceable></option></term>
 
302
      <listitem>
 
303
       <para>
 
304
        Restore only objects that are in the named schema.  This can be
 
305
        combined with the <option>-t</option> option to restore just a
 
306
        specific table.
 
307
       </para>
 
308
      </listitem>
 
309
     </varlistentry>
 
310
 
 
311
     <varlistentry>
 
312
      <term><option>-O</option></term>
 
313
      <term><option>--no-owner</option></term>
 
314
      <listitem>
 
315
       <para>
 
316
        Do not output commands to set
 
317
        ownership of objects to match the original database.
 
318
        By default, <application>pg_restore</application> issues
 
319
        <command>ALTER OWNER</> or
 
320
        <command>SET SESSION AUTHORIZATION</command>
 
321
        statements to set ownership of created schema elements.
 
322
        These statements will fail unless the initial connection to the
 
323
        database is made by a superuser
 
324
        (or the same user that owns all of the objects in the script).
 
325
        With <option>-O</option>, any user name can be used for the
 
326
        initial connection, and this user will own all the created objects.
 
327
       </para>
 
328
      </listitem>
 
329
     </varlistentry>
 
330
 
 
331
     <varlistentry>
 
332
      <term><option>--no-tablespaces</option></term>
 
333
      <listitem>
 
334
       <para>
 
335
        Do not output commands to select tablespaces.
 
336
        With this option, all objects will be created in whichever
 
337
        tablespace is the default during restore.
 
338
       </para>
 
339
      </listitem>
 
340
     </varlistentry>
 
341
 
 
342
     <varlistentry>
 
343
      <term><option>--no-security-label</option></term>
 
344
      <listitem>
 
345
       <para>
 
346
        Do not output commands to restore security labels,
 
347
        even if the archive contains them.
 
348
       </para>
 
349
      </listitem>
 
350
     </varlistentry>
 
351
 
 
352
     <varlistentry>
 
353
      <term><option>-P <replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
 
354
      <term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
 
355
      <listitem>
 
356
       <para>
 
357
        Restore the named function only.  Be careful to spell the function
 
358
        name and arguments exactly as they appear in the dump file's table
 
359
        of contents.
 
360
       </para>
 
361
      </listitem>
 
362
     </varlistentry>
 
363
 
 
364
     <varlistentry>
 
365
      <term><option>-R</option></term>
 
366
      <term><option>--no-reconnect</option></term>
 
367
      <listitem>
 
368
       <para>
 
369
        This option is obsolete but still accepted for backwards
 
370
        compatibility.
 
371
       </para>
 
372
      </listitem>
 
373
     </varlistentry>
 
374
 
 
375
     <varlistentry>
 
376
      <term><option>-s</option></term>
 
377
      <term><option>--schema-only</option></term>
 
378
      <listitem>
 
379
       <para>
 
380
        Restore only the schema (data definitions), not the data (table
 
381
        contents).  Current sequence values will not be restored, either.
 
382
        (Do not confuse this with the <option>--schema</> option, which
 
383
        uses the word <quote>schema</> in a different meaning.)
 
384
       </para>
 
385
      </listitem>
 
386
     </varlistentry>
 
387
 
 
388
     <varlistentry>
 
389
      <term><option>-S <replaceable class="parameter">username</replaceable></option></term>
 
390
      <term><option>--superuser=<replaceable class="parameter">username</replaceable></option></term>
 
391
      <listitem>
 
392
       <para>
 
393
        Specify the superuser user name to use when disabling triggers.
 
394
        This is only relevant if <option>--disable-triggers</> is used.
 
395
       </para>
 
396
      </listitem>
 
397
     </varlistentry>
 
398
 
 
399
     <varlistentry>
 
400
      <term><option>-t <replaceable class="parameter">table</replaceable></option></term>
 
401
      <term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
 
402
      <listitem>
 
403
       <para>
 
404
        Restore definition and/or data of named table only.  This can be
 
405
        combined with the <option>-n</option> option to specify a schema.
 
406
       </para>
 
407
      </listitem>
 
408
     </varlistentry>
 
409
 
 
410
     <varlistentry>
 
411
      <term><option>-T <replaceable class="parameter">trigger</replaceable></option></term>
 
412
      <term><option>--trigger=<replaceable class="parameter">trigger</replaceable></option></term>
 
413
      <listitem>
 
414
       <para>
 
415
        Restore named trigger only.
 
416
       </para>
 
417
      </listitem>
 
418
     </varlistentry>
 
419
 
 
420
     <varlistentry>
 
421
      <term><option>-v</option></term>
 
422
      <term><option>--verbose</option></term>
 
423
      <listitem>
 
424
       <para>
 
425
        Specifies verbose mode.
 
426
       </para>
 
427
      </listitem>
 
428
     </varlistentry>
 
429
 
 
430
     <varlistentry>
 
431
       <term><option>-V</></term>
 
432
       <term><option>--version</></term>
 
433
       <listitem>
 
434
       <para>
 
435
       Print the <application>pg_restore</application> version and exit.
 
436
       </para>
 
437
       </listitem>
 
438
     </varlistentry>
 
439
 
 
440
     <varlistentry>
 
441
      <term><option>-x</option></term>
 
442
      <term><option>--no-privileges</option></term>
 
443
      <term><option>--no-acl</option></term>
 
444
      <listitem>
 
445
       <para>
 
446
        Prevent restoration of access privileges (grant/revoke commands).
 
447
       </para>
 
448
      </listitem>
 
449
     </varlistentry>
 
450
 
 
451
     <varlistentry>
 
452
      <term><option>--disable-triggers</></term>
 
453
      <listitem>
 
454
       <para>
 
455
        This option is only relevant when performing a data-only restore.
 
456
        It instructs <application>pg_restore</application> to execute commands
 
457
        to temporarily disable triggers on the target tables while
 
458
        the data is reloaded.  Use this if you have referential
 
459
        integrity checks or other triggers on the tables that you
 
460
        do not want to invoke during data reload.
 
461
       </para>
 
462
 
 
463
       <para>
 
464
        Presently, the commands emitted for
 
465
        <option>--disable-triggers</> must be done as superuser.  So, you
 
466
        should also specify a superuser name with <option>-S</>, or
 
467
        preferably run <application>pg_restore</application> as a
 
468
        <productname>PostgreSQL</> superuser.
 
469
       </para>
 
470
      </listitem>
 
471
     </varlistentry>
 
472
 
 
473
     <varlistentry>
 
474
      <term><option>--use-set-session-authorization</option></term>
 
475
      <listitem>
 
476
       <para>
 
477
        Output SQL-standard <command>SET SESSION AUTHORIZATION</> commands
 
478
        instead of <command>ALTER OWNER</> commands to determine object
 
479
        ownership.  This makes the dump more standards-compatible, but
 
480
        depending on the history of the objects in the dump, might not restore
 
481
        properly.
 
482
       </para>
 
483
      </listitem>
 
484
     </varlistentry>
 
485
 
 
486
     <varlistentry>
 
487
      <term><option>--no-data-for-failed-tables</option></term>
 
488
      <listitem>
 
489
       <para>
 
490
        By default, table data is restored even if the creation command
 
491
        for the table failed (e.g., because it already exists).
 
492
        With this option, data for such a table is skipped.
 
493
        This behavior is useful if the target database already
 
494
        contains the desired table contents.  For example,
 
495
        auxiliary tables for <productname>PostgreSQL</> extensions
 
496
        such as <productname>PostGIS</> might already be loaded in
 
497
        the target database; specifying this option prevents duplicate
 
498
        or obsolete data from being loaded into them.
 
499
       </para>
 
500
 
 
501
       <para>
 
502
        This option is effective only when restoring directly into a
 
503
        database, not when producing SQL script output.
 
504
       </para>
 
505
      </listitem>
 
506
     </varlistentry>
 
507
 
 
508
     <varlistentry>
 
509
      <term><option>-1</option></term>
 
510
      <term><option>--single-transaction</option></term>
 
511
      <listitem>
 
512
       <para>
 
513
        Execute the restore as a single transaction (that is, wrap the
 
514
        emitted commands in <command>BEGIN</>/<command>COMMIT</>).  This
 
515
        ensures that either all the commands complete successfully, or no
 
516
        changes are applied. This option implies
 
517
        <option>--exit-on-error</>.
 
518
       </para>
 
519
      </listitem>
 
520
     </varlistentry>
 
521
 
 
522
     <varlistentry>
 
523
       <term><option>-?</></term>
 
524
       <term><option>--help</></term>
 
525
       <listitem>
 
526
       <para>
 
527
       Show help about <application>pg_restore</application> command line
 
528
       arguments, and exit.
 
529
       </para>
 
530
       </listitem>
 
531
     </varlistentry>
 
532
 
 
533
    </variablelist>
 
534
   </para>
 
535
 
 
536
   <para>
 
537
    <application>pg_restore</application> also accepts
 
538
    the following command line arguments for connection parameters:
 
539
 
 
540
    <variablelist>
 
541
     <varlistentry>
 
542
      <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
 
543
      <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
 
544
      <listitem>
 
545
       <para>
 
546
        Specifies the host name of the machine on which the server is
 
547
        running.  If the value begins with a slash, it is used as the
 
548
        directory for the Unix domain socket. The default is taken
 
549
        from the <envar>PGHOST</envar> environment variable, if set,
 
550
        else a Unix domain socket connection is attempted.
 
551
       </para>
 
552
      </listitem>
 
553
     </varlistentry>
 
554
 
 
555
     <varlistentry>
 
556
      <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
 
557
      <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
 
558
      <listitem>
 
559
       <para>
 
560
        Specifies the TCP port or local Unix domain socket file
 
561
        extension on which the server is listening for connections.
 
562
        Defaults to the <envar>PGPORT</envar> environment variable, if
 
563
        set, or a compiled-in default.
 
564
        </para>
 
565
      </listitem>
 
566
     </varlistentry>
 
567
 
 
568
     <varlistentry>
 
569
      <term><option>-U <replaceable>username</replaceable></option></term>
 
570
      <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
 
571
      <listitem>
 
572
       <para>
 
573
        User name to connect as.
 
574
       </para>
 
575
      </listitem>
 
576
     </varlistentry>
 
577
 
 
578
     <varlistentry>
 
579
      <term><option>-w</></term>
 
580
      <term><option>--no-password</></term>
 
581
      <listitem>
 
582
       <para>
 
583
        Never issue a password prompt.  If the server requires
 
584
        password authentication and a password is not available by
 
585
        other means such as a <filename>.pgpass</filename> file, the
 
586
        connection attempt will fail.  This option can be useful in
 
587
        batch jobs and scripts where no user is present to enter a
 
588
        password.
 
589
       </para>
 
590
      </listitem>
 
591
     </varlistentry>
 
592
 
 
593
     <varlistentry>
 
594
      <term><option>-W</option></term>
 
595
      <term><option>--password</option></term>
 
596
      <listitem>
 
597
       <para>
 
598
        Force <application>pg_restore</application> to prompt for a
 
599
        password before connecting to a database.
 
600
       </para>
 
601
 
 
602
       <para>
 
603
        This option is never essential, since
 
604
        <application>pg_restore</application> will automatically prompt
 
605
        for a password if the server demands password authentication.
 
606
        However, <application>pg_restore</application> will waste a
 
607
        connection attempt finding out that the server wants a password.
 
608
        In some cases it is worth typing <option>-W</> to avoid the extra
 
609
        connection attempt.
 
610
       </para>
 
611
      </listitem>
 
612
     </varlistentry>
 
613
 
 
614
     <varlistentry>
 
615
      <term><option>--role=<replaceable class="parameter">rolename</replaceable></option></term>
 
616
      <listitem>
 
617
       <para>
 
618
        Specifies a role name to be used to perform the restore.
 
619
        This option causes <application>pg_restore</> to issue a
 
620
        <command>SET ROLE</> <replaceable class="parameter">rolename</>
 
621
        command after connecting to the database. It is useful when the
 
622
        authenticated user (specified by <option>-U</>) lacks privileges
 
623
        needed by <application>pg_restore</>, but can switch to a role with
 
624
        the required rights.  Some installations have a policy against
 
625
        logging in directly as a superuser, and use of this option allows
 
626
        restores to be performed without violating the policy.
 
627
       </para>
 
628
      </listitem>
 
629
     </varlistentry>
 
630
 
 
631
    </variablelist>
 
632
   </para>
 
633
 </refsect1>
 
634
 
 
635
 
 
636
 <refsect1>
 
637
  <title>Environment</title>
 
638
 
 
639
  <variablelist>
 
640
   <varlistentry>
 
641
    <term><envar>PGHOST</envar></term>
 
642
    <term><envar>PGOPTIONS</envar></term>
 
643
    <term><envar>PGPORT</envar></term>
 
644
    <term><envar>PGUSER</envar></term>
 
645
 
 
646
    <listitem>
 
647
     <para>
 
648
      Default connection parameters
 
649
     </para>
 
650
    </listitem>
 
651
   </varlistentry>
 
652
  </variablelist>
 
653
 
 
654
  <para>
 
655
   This utility, like most other <productname>PostgreSQL</> utilities,
 
656
   also uses the environment variables supported by <application>libpq</>
 
657
   (see <xref linkend="libpq-envars">).
 
658
  </para>
 
659
 
 
660
 </refsect1>
 
661
 
 
662
 
 
663
 <refsect1 id="app-pgrestore-diagnostics">
 
664
  <title>Diagnostics</title>
 
665
 
 
666
  <para>
 
667
   When a direct database connection is specified using the
 
668
   <option>-d</option> option, <application>pg_restore</application>
 
669
   internally executes <acronym>SQL</acronym> statements. If you have
 
670
   problems running <application>pg_restore</application>, make sure
 
671
   you are able to select information from the database using, for
 
672
   example, <xref linkend="app-psql">.  Also, any default connection
 
673
   settings and environment variables used by the
 
674
   <application>libpq</application> front-end library will apply.
 
675
  </para>
 
676
 </refsect1>
 
677
 
 
678
 
 
679
 <refsect1 id="app-pgrestore-notes">
 
680
  <title>Notes</title>
 
681
 
 
682
  <para>
 
683
   If your installation has any local additions to the
 
684
   <literal>template1</> database, be careful to load the output of
 
685
   <application>pg_restore</application> into a truly empty database;
 
686
   otherwise you are likely to get errors due to duplicate definitions
 
687
   of the added objects.  To make an empty database without any local
 
688
   additions, copy from <literal>template0</> not <literal>template1</>, for example:
 
689
<programlisting>
 
690
CREATE DATABASE foo WITH TEMPLATE template0;
 
691
</programlisting>
 
692
  </para>
 
693
 
 
694
  <para>
 
695
   The limitations of <application>pg_restore</application> are detailed below.
 
696
 
 
697
   <itemizedlist>
 
698
    <listitem>
 
699
     <para>
 
700
      When restoring data to a pre-existing table and the option
 
701
      <option>--disable-triggers</> is used,
 
702
      <application>pg_restore</application> emits commands
 
703
      to disable triggers on user tables before inserting the data, then emits commands to
 
704
      re-enable them after the data has been inserted.  If the restore is stopped in the
 
705
      middle, the system catalogs might be left in the wrong state.
 
706
     </para>
 
707
    </listitem>
 
708
 
 
709
    <listitem>
 
710
     <para>
 
711
      <application>pg_restore</application> cannot restore large objects
 
712
      selectively;  for instance, only those for a specific table.  If
 
713
      an archive contains large objects, then all large objects will be
 
714
      restored, or none of them if they are excluded via <option>-L</option>,
 
715
      <option>-t</option>, or other options.
 
716
     </para>
 
717
    </listitem>
 
718
 
 
719
   </itemizedlist>
 
720
  </para>
 
721
 
 
722
  <para>
 
723
   See also the <xref linkend="app-pgdump"> documentation for details on
 
724
   limitations of <application>pg_dump</application>.
 
725
  </para>
 
726
 
 
727
  <para>
 
728
   Once restored, it is wise to run <command>ANALYZE</> on each
 
729
   restored table so the optimizer has useful statistics; see
 
730
   <xref linkend="vacuum-for-statistics"> and
 
731
   <xref linkend="autovacuum"> for more information.
 
732
  </para>
 
733
 
 
734
 </refsect1>
 
735
 
 
736
 
 
737
 <refsect1 id="app-pgrestore-examples">
 
738
  <title>Examples</title>
 
739
 
 
740
  <para>
 
741
   Assume we have dumped a database called <literal>mydb</> into a
 
742
   custom-format dump file:
 
743
 
 
744
<screen>
 
745
<prompt>$</prompt> <userinput>pg_dump -Fc mydb &gt; db.dump</userinput>
 
746
</screen>
 
747
  </para>
 
748
 
 
749
  <para>
 
750
   To drop the database and recreate it from the dump:
 
751
 
 
752
<screen>
 
753
<prompt>$</prompt> <userinput>dropdb mydb</userinput>
 
754
<prompt>$</prompt> <userinput>pg_restore -C -d postgres db.dump</userinput>
 
755
</screen>
 
756
 
 
757
   The database named in the <option>-d</> switch can be any database existing
 
758
   in the cluster; <application>pg_restore</> only uses it to issue the
 
759
   <command>CREATE DATABASE</> command for <literal>mydb</>.  With
 
760
   <option>-C</>, data is always restored into the database name that appears
 
761
   in the dump file.
 
762
  </para>
 
763
 
 
764
  <para>
 
765
   To reload the dump into a new database called <literal>newdb</>:
 
766
 
 
767
<screen>
 
768
<prompt>$</prompt> <userinput>createdb -T template0 newdb</userinput>
 
769
<prompt>$</prompt> <userinput>pg_restore -d newdb db.dump</userinput>
 
770
</screen>
 
771
 
 
772
   Notice we don't use <option>-C</>, and instead connect directly to the
 
773
   database to be restored into.  Also note that we clone the new database
 
774
   from <literal>template0</> not <literal>template1</>, to ensure it is
 
775
   initially empty.
 
776
  </para>
 
777
 
 
778
  <para>
 
779
   To reorder database items, it is first necessary to dump the table of
 
780
   contents of the archive:
 
781
<screen>
 
782
<prompt>$</prompt> <userinput>pg_restore -l db.dump &gt; db.list</userinput>
 
783
</screen>
 
784
   The listing file consists of a header and one line for each item, e.g.:
 
785
<programlisting>
 
786
;
 
787
; Archive created at Mon Sep 14 13:55:39 2009
 
788
;     dbname: DBDEMOS
 
789
;     TOC Entries: 81
 
790
;     Compression: 9
 
791
;     Dump Version: 1.10-0
 
792
;     Format: CUSTOM
 
793
;     Integer: 4 bytes
 
794
;     Offset: 8 bytes
 
795
;     Dumped from database version: 8.3.5
 
796
;     Dumped by pg_dump version: 8.3.8
 
797
;
 
798
;
 
799
; Selected TOC Entries:
 
800
;
 
801
3; 2615 2200 SCHEMA - public pasha
 
802
1861; 0 0 COMMENT - SCHEMA public pasha
 
803
1862; 0 0 ACL - public pasha
 
804
317; 1247 17715 TYPE public composite pasha
 
805
319; 1247 25899 DOMAIN public domain0 pasha
 
806
</programlisting>
 
807
   Semicolons start a comment, and the numbers at the start of lines refer to the
 
808
   internal archive ID assigned to each item.
 
809
  </para>
 
810
 
 
811
  <para>
 
812
   Lines in the file can be commented out, deleted, and reordered. For example:
 
813
<programlisting>
 
814
10; 145433 TABLE map_resolutions postgres
 
815
;2; 145344 TABLE species postgres
 
816
;4; 145359 TABLE nt_header postgres
 
817
6; 145402 TABLE species_records postgres
 
818
;8; 145416 TABLE ss_old postgres
 
819
</programlisting>
 
820
   could be used as input to <application>pg_restore</application> and would only restore
 
821
   items 10 and 6, in that order:
 
822
<screen>
 
823
<prompt>$</prompt> <userinput>pg_restore -L db.list db.dump</userinput>
 
824
</screen>
 
825
  </para>
 
826
 
 
827
 </refsect1>
 
828
 
 
829
 <refsect1>
 
830
  <title>See Also</title>
 
831
 
 
832
  <simplelist type="inline">
 
833
   <member><xref linkend="app-pgdump"></member>
 
834
   <member><xref linkend="app-pg-dumpall"></member>
 
835
   <member><xref linkend="app-psql"></member>
 
836
  </simplelist>
 
837
 </refsect1>
 
838
</refentry>