]> granicus.if.org Git - postgresql/blob - doc/src/sgml/pgupgrade.sgml
4f263fe6720a162f7b5ae472f403812e61eabcb6
[postgresql] / doc / src / sgml / pgupgrade.sgml
1 <!-- doc/src/sgml/pgupgrade.sgml -->
2
3 <sect1 id="pgupgrade" xreflabel="pg_upgrade">
4  <title>pg_upgrade</title>
5
6  <indexterm zone="pgupgrade">
7   <primary>pg_upgrade</primary>
8  </indexterm>
9
10  <para>
11   <application>pg_upgrade</> (formerly called <application>pg_migrator</>) allows data
12   stored in <productname>PostgreSQL</> data files to be upgraded to a later <productname>PostgreSQL</>
13   major version without the data dump/reload typically required for
14   major version upgrades, e.g. from 8.4.7 to the current major release
15   of <productname>PostgreSQL</>.  It is not required for minor version upgrades, e.g. from
16   9.0.1 to 9.0.4.
17  </para>
18
19  <para>
20   Major PostgreSQL releases regularly add new features that often
21   change the layout of the system tables, but the internal data storage
22   format rarely changes.  <application>pg_upgrade</> uses this fact
23   to perform rapid upgrades by creating new system tables and simply
24   reusing the old user data files.  If a future major release ever
25   changes the data storage format in a way that makes the old data
26   format unreadable, <application>pg_upgrade</> will not be usable
27   for such upgrades.  (The community will attempt to avoid such
28   situations.)
29  </para>
30
31  <para>
32   <application>pg_upgrade</> does its best to
33   make sure the old and new clusters are binary-compatible, e.g.  by
34   checking for compatible compile-time settings, including 32/64-bit
35   binaries.  It is important that
36   any external modules are also binary compatible, though this cannot
37   be checked by <application>pg_upgrade</>.
38  </para>
39
40  <sect2>
41   <title>Supported Versions</title>
42
43   <para>
44    pg_upgrade supports upgrades from 8.3.X and later to the current
45    major release of <productname>PostgreSQL</>, including snapshot and alpha releases.
46
47   </para>
48
49  </sect2>
50
51  <sect2>
52   <title><application>pg_upgrade</> Options</title>
53
54    <para>
55     <application>pg_upgrade</application> accepts the following command-line arguments:
56
57     <variablelist>
58
59      <varlistentry>
60       <term><option>-b</option> <replaceable>old_bindir</></term>
61       <term><option>--old-bindir=</option><replaceable>old_bindir</></term>
62       <listitem><para>the old cluster executable directory;
63       environment variable <envar>PGBINOLD</></para></listitem>
64      </varlistentry>
65
66      <varlistentry>
67       <term><option>-B</option> <replaceable>new_bindir</></term>
68       <term><option>--new-bindir=</option><replaceable>new_bindir</></term>
69       <listitem><para>the new cluster executable directory;
70       environment variable <envar>PGBINNEW</></para></listitem>
71      </varlistentry>
72
73      <varlistentry>
74       <term><option>-c</option></term>
75       <term><option>--check</option></term>
76       <listitem><para>check clusters only, don't change any data</para></listitem>
77      </varlistentry>
78
79      <varlistentry>
80       <term><option>-d</option> <replaceable>old_datadir</></term>
81       <term><option>--old-datadir=</option><replaceable>old_datadir</></term>
82       <listitem><para>the old cluster data directory; environment
83       variable <envar>PGDATAOLD</></para></listitem>
84      </varlistentry>
85
86      <varlistentry>
87       <term><option>-D</option> <replaceable>new_datadir</></term>
88       <term><option>--new-datadir=</option><replaceable>new_datadir</></term>
89       <listitem><para>the new cluster data directory; environment
90       variable <envar>PGDATANEW</></para></listitem>
91      </varlistentry>
92
93      <varlistentry>
94       <term><option>-g</option></term>
95       <term><option>--debug</option></term>
96       <listitem><para>enable debugging</para></listitem>
97      </varlistentry>
98
99      <varlistentry>
100       <term><option>-G</option> <replaceable>debug_filename</></term>
101       <term><option>--debugfile=</option><replaceable>debug_filename</></term>
102       <listitem><para>output debugging activity to file</para></listitem>
103      </varlistentry>
104
105      <varlistentry>
106       <term><option>-k</option></term>
107       <term><option>--link</option></term>
108       <listitem><para>use hard links instead of copying files to the new cluster</para></listitem>
109      </varlistentry>
110
111      <varlistentry>
112       <term><option>-l</option> <replaceable>log_filename</></term>
113       <term><option>--logfile=</option><replaceable>log_filename</></term>
114       <listitem><para>log internal activity to file</para></listitem>
115      </varlistentry>
116
117      <varlistentry>
118       <term><option>-o</option> <replaceable class="parameter">options</replaceable></term>
119       <term><option>--old-options</option> <replaceable class="parameter">options</replaceable></term>
120       <listitem><para>options to be passed directly to the
121       old <command>postgres</command> command</para></listitem>
122      </varlistentry>
123
124      <varlistentry>
125       <term><option>-O</option> <replaceable class="parameter">options</replaceable></term>
126       <term><option>--new-options</option> <replaceable class="parameter">options</replaceable></term>
127       <listitem><para>options to be passed directly to the
128       new <command>postgres</command> command</para></listitem>
129      </varlistentry>
130
131      <varlistentry>
132       <term><option>-p</option> <replaceable>old_port_number</></term>
133       <term><option>--old-port=</option><replaceable>old_portnum</></term>
134       <listitem><para>the old cluster port number; environment
135       variable <envar>PGPORTOLD</></para></listitem>
136      </varlistentry>
137
138      <varlistentry>
139       <term><option>-P</option> <replaceable>new_port_number</></term>
140       <term><option>--new-port=</option><replaceable>new_portnum</></term>
141       <listitem><para>the new cluster port number; environment
142       variable <envar>PGPORTNEW</></para></listitem>
143      </varlistentry>
144
145      <varlistentry>
146       <term><option>-u</option> <replaceable>user_name</></term>
147       <term><option>--user=</option><replaceable>user_name</></term>
148       <listitem><para>cluster's super user name; environment
149       variable <envar>PGUSER</></para></listitem>
150      </varlistentry>
151
152      <varlistentry>
153       <term><option>-v</option></term>
154       <term><option>--verbose</option></term>
155       <listitem><para>enable verbose output</para></listitem>
156      </varlistentry>
157
158      <varlistentry>
159       <term><option>-V</option></term>
160       <term><option>--version</option></term>
161       <listitem><para>display version information, then exit</para></listitem>
162      </varlistentry>
163
164      <varlistentry>
165       <term><option>-?</option></term>
166       <term><option>-h</option></term>
167       <term><option>--help</option></term>
168       <listitem><para>show help, then exit</para></listitem>
169      </varlistentry>
170
171     </variablelist>
172    </para>
173
174  </sect2>
175
176  <sect2>
177   <title>Upgrade Steps</title>
178
179   <procedure>
180    <step performance="optional">
181     <title>Optionally move the old cluster</title>
182
183     <para>
184      If you are using a version-specific installation directory, e.g.
185      <filename>/opt/PostgreSQL/9.1</>, you do not need to move the old cluster. The
186      one-click installers all use version-specific installation directories.
187     </para>
188
189     <para>
190      If your installation directory is not version-specific, e.g.
191      <filename>/usr/local/pgsql</>, it is necessary to move the current PostgreSQL install
192      directory so it does not interfere with the new <productname>PostgreSQL</> installation.
193      Once the current <productname>PostgreSQL</> server is shut down, it is safe to rename the
194      PostgreSQL installation directory; assuming the old directory is
195      <filename>/usr/local/pgsql</>, you can do:
196
197 <programlisting>
198 mv /usr/local/pgsql /usr/local/pgsql.old
199 </programlisting>
200      to rename the directory.
201     </para>
202    </step>
203
204    <step>
205     <title>For source installs, build the new version</title>
206
207     <para>
208      Build the new PostgreSQL source with <command>configure</> flags that are compatible
209      with the old cluster. <application>pg_upgrade</> will check <command>pg_controldata</> to make
210      sure all settings are compatible before starting the upgrade.
211     </para>
212    </step>
213
214    <step>
215     <title>Install the new PostgreSQL binaries</title>
216
217     <para>
218      Install the new server's binaries and support files.
219     </para>
220
221     <para>
222      For source installs, if you wish to install the new server in a custom
223      location, use the <literal>prefix</literal> variable:
224
225 <programlisting>
226 gmake prefix=/usr/local/pgsql.new install
227 </programlisting>
228     </para>
229    </step>
230
231    <step>
232     <title>Install pg_upgrade and pg_upgrade_support</title>
233
234     <para>
235      Install the <application>pg_upgrade</> binary and
236      <application>pg_upgrade_support</> library in the new PostgreSQL cluster.
237     </para>
238    </step>
239
240    <step>
241     <title>Initialize the new PostgreSQL cluster</title>
242
243     <para>
244      Initialize the new cluster using <command>initdb</command>.
245      Again, use compatible <command>initdb</command>
246      flags that match the old cluster. Many
247      prebuilt installers do this step automatically. There is no need to
248      start the new cluster.
249     </para>
250    </step>
251
252    <step>
253     <title>Install custom shared object files</title>
254
255     <para>
256      Install any custom shared object files (or DLLs) used by the old cluster
257      into the new cluster, e.g. <filename>pgcrypto.so</filename>, 
258      whether they are from <filename>contrib</filename>
259      or some other source. Do not install the schema definitions, e.g.
260      <filename>pgcrypto.sql</>, because these will be upgraded from the old cluster.
261     </para>
262    </step>
263
264    <step>
265     <title>Adjust authentication</title>
266
267     <para>
268      <command>pg_upgrade</> will connect to the old and new servers several times,
269      so you might want to set authentication to <literal>trust</> in
270      <filename>pg_hba.conf</>, or if using <literal>md5</> authentication,
271      use a <filename>~/.pgpass</> file (see <xref linkend="libpq-pgpass">)
272      to avoid being prompted repeatedly for a password.
273     </para>
274    </step>
275
276    <step>
277     <title>Stop both servers</title>
278
279     <para>
280      Make sure both database servers are stopped using, on Unix, e.g.:
281
282 <programlisting>
283 pg_ctl -D /opt/PostgreSQL/8.4 stop
284 pg_ctl -D /opt/PostgreSQL/9.0 stop
285 </programlisting>
286
287      or on Windows, using the proper service names:
288
289 <programlisting>
290 NET STOP postgresql-8.4
291 NET STOP postgresql-9.0
292 </programlisting>
293
294      or
295
296 <programlisting>
297 NET STOP pgsql-8.3  (<productname>PostgreSQL</> 8.3 and older used a different service name)
298 </programlisting>
299     </para>
300    </step>
301
302    <step>
303     <title>Run <application>pg_upgrade</></title>
304
305     <para>
306      Always run the <application>pg_upgrade</> binary of the new server, not the old one.
307      <application>pg_upgrade</> requires the specification of the old and new cluster's
308      data and executable (<filename>bin</>) directories. You can also specify
309      user and port values, and whether you want the data linked instead of
310      copied (the default).
311     </para>
312
313     <para>
314      If you use link mode, the upgrade will be much faster (no file
315      copying), but you will not be able to access your old cluster
316      once you start the new cluster after the upgrade.  Link mode also
317      requires that the old and new cluster data directories be in the
318      same file system.  See <literal>pg_upgrade --help</> for a full
319      list of options.
320    </para>
321
322    <para>
323    </para>
324
325     <para>
326      For Windows users, you must be logged into an administrative account, and
327      then start a shell as the <literal>postgres</> user and set the proper path:
328
329 <programlisting>
330 RUNAS /USER:postgres "CMD.EXE"
331 SET PATH=%PATH%;C:\Program Files\PostgreSQL\9.0\bin;
332 </programlisting>
333
334      and then run <application>pg_upgrade</> with quoted directories, e.g.:
335
336 <programlisting>
337 pg_upgrade.exe
338         --old-datadir "C:/Program Files/PostgreSQL/8.4/data"
339         --new-datadir "C:/Program Files/PostgreSQL/9.0/data"
340         --old-bindir "C:/Program Files/PostgreSQL/8.4/bin"
341         --new-bindir "C:/Program Files/PostgreSQL/9.0/bin"
342 </programlisting>
343
344      Once started, <command>pg_upgrade</> will verify the two clusters are compatible
345      and then do the upgrade. You can use <command>pg_upgrade --check</>
346      to perform only the checks, even if the old server is still
347      running. <command>pg_upgrade --check</> will also outline any
348      manual adjustments you will need to make after the upgrade.
349      <command>pg_upgrade</> requires write permission in the current directory.
350     </para>
351
352     <para>
353      Obviously, no one should be accessing the clusters during the
354      upgrade.  <application>pg_upgrade</> defaults to running servers
355      on port 50432 to avoid unintended client connections.
356      You can use the same port numbers for both clusters because the
357      old and new clusters will not be running at the same time.
358     </para>
359
360     <para>
361      If an error occurs while restoring the database schema, <command>pg_upgrade</> will
362      exit and you will have to revert to the old cluster as outlined in <xref linkend="pgupgrade-step-revert">
363      below. To try <command>pg_upgrade</command> again, you will need to modify the old
364      cluster so the pg_upgrade schema restore succeeds. If the problem is a
365      contrib module, you might need to uninstall the contrib module from
366      the old cluster and install it in the new cluster after the upgrade,
367      assuming the module is not being used to store user data.
368     </para>
369    </step>
370
371    <step>
372     <title>Restore <filename>pg_hba.conf</></title>
373
374     <para>
375      If you modified <filename>pg_hba.conf</> to use <literal>trust</>,
376      restore its original authentication settings.
377     </para>
378    </step>
379
380    <step>
381     <title>Post-Upgrade processing</title>
382
383     <para>
384      If any post-upgrade processing is required, pg_upgrade will issue
385      warnings as it completes. It will also generate script files that must
386      be run by the administrator. The script files will connect to each
387      database that needs post-upgrade processing. Each script should be
388      run using:
389
390 <programlisting>
391 psql --username postgres --file script.sql postgres
392 </programlisting>
393
394      The scripts can be run in any order and can be deleted once they have
395      been run.
396     </para>
397
398     <caution>
399     <para>
400      In general it is unsafe to access tables referenced in rebuild scripts
401      until the rebuild scripts have run to completion; doing so could yield
402      incorrect results or poor performance. Tables not referenced in rebuild
403      scripts can be accessed immediately.
404     </para>
405     </caution>
406    </step>
407
408    <step>
409     <title>Statistics</title>
410
411     <para>
412      Because optimizer statistics are not transferred by <command>pg_upgrade</>, you will
413      be instructed to run a command to regenerate that information at the end
414      of the upgrade.
415     </para>
416    </step>
417
418    <step>
419     <title>Delete old cluster</title>
420
421     <para>
422      Once you are satisfied with the upgrade, you can delete the old
423      cluster's data directories by running the script mentioned when
424      <command>pg_upgrade</command> completes. You can also delete the
425      old installation directories
426      (e.g. <filename>bin</>, <filename>share</>).
427     </para>
428    </step>
429
430    <step id="pgupgrade-step-revert" performance="optional">
431     <title>Reverting to old cluster</title>
432
433     <para>
434      If, after running <command>pg_upgrade</command>, you wish to revert to the old cluster,
435      there are several options:
436
437      <itemizedlist>
438       <listitem>
439        <para>
440         If you ran <command>pg_upgrade</command>
441         with <option>--check</>, no modifications were made to the old
442         cluster and you can re-use it anytime.
443        </para>
444       </listitem>
445
446       <listitem>
447        <para>
448         If you ran <command>pg_upgrade</command>
449         with <option>--link</>, the data files are shared between the
450         old and new cluster. If you started the new cluster, the new
451         server has written to those shared files and it is unsafe to
452         use the old cluster.
453        </para>
454       </listitem>
455
456       <listitem>
457        <para>
458         If you ran <command>pg_upgrade</command> <emphasis>without</>
459         <option>--link</> or did not start the new server, the
460         old cluster was not modified except that, if linking
461         started, a <literal>.old</> suffix was appended to
462         <filename>$PGDATA/global/pg_control</>.  To reuse the old
463         cluster, possibly remove the <filename>.old</> suffix from
464         <filename>$PGDATA/global/pg_control</>; you can then restart the
465         old cluster.
466        </para>
467       </listitem>
468      </itemizedlist>
469     </para>
470    </step>
471   </procedure>
472
473  </sect2>
474
475  <sect2>
476   <title>Limitations in Upgrading <emphasis>from</> PostgreSQL 8.3</title>
477
478   <para>
479    Upgrading from PostgreSQL 8.3 has additional restrictions not present
480    when upgrading from later PostgreSQL releases.  For example,
481    pg_upgrade will not work for upgrading from 8.3 if a user column
482    is defined as:
483    <itemizedlist>
484     <listitem>
485      <para>
486       a <type>tsquery</> data type
487      </para>
488     </listitem>
489     <listitem>
490      <para>
491       data type <type>name</> and is not the first column
492      </para>
493     </listitem>
494    </itemizedlist>
495   </para>
496
497   <para>
498    You must drop any such columns and upgrade them manually.
499   </para>
500
501   <para>
502    pg_upgrade will not work if the <filename>ltree</>
503    contrib module is installed in a database.
504   </para>
505
506   <para>
507    pg_upgrade will require a table rebuild if:
508    <itemizedlist>
509     <listitem>
510      <para>
511       a user column is of data type <type>tsvector</type>
512      </para>
513     </listitem>
514    </itemizedlist>
515   </para>
516
517   <para>
518    pg_upgrade will require a reindex if:
519    <itemizedlist>
520     <listitem>
521      <para>
522       an index is of type hash or GIN
523      </para>
524     </listitem>
525     <listitem>
526      <para>
527       an index uses <function>bpchar_pattern_ops</>
528      </para>
529     </listitem>
530    </itemizedlist>
531   </para>
532
533   <para>
534    Also, the default datetime storage format changed to integer after
535    <productname>PostgreSQL</> 8.3. pg_upgrade will check that the datetime storage format
536    used by the old and new clusters match. Make sure your new cluster is
537    built with the configure flag <option>--disable-integer-datetimes</>.
538   </para>
539
540   <para>
541    For Windows users, note that due to different integer datetimes settings
542    used by the one-click installer and the MSI installer, it is only
543    possible to upgrade from version 8.3 of the one-click distribution to
544    version 8.4 or later of the one-click distribution. It is not
545    possible to upgrade from the MSI installer to the one-click installer.
546   </para>
547
548  </sect2>
549
550  <sect2>
551   <title>Notes</title>
552
553   <para>
554    <application>pg_upgrade</> does not support upgrading of databases
555    containing these <type>reg*</> OID-referencing system data types:
556    <type>regproc</>, <type>regprocedure</>, <type>regoper</>,
557    <type>regoperator</>, <type>regconfig</>, and
558    <type>regdictionary</>.  (<type>regtype</> can be upgraded.)
559   </para>
560
561   <para>
562    All failure, rebuild, and reindex cases will be reported by
563    <application>pg_upgrade</> if they affect your installation;
564    post-upgrade scripts to rebuild tables and indexes will be
565    generated automatically.
566   </para>
567
568   <para>
569    For deployment testing, create a schema-only copy of the old cluster,
570    insert dummy data, and upgrade that.
571   </para>
572
573   <para>
574    If you are upgrading a pre-<productname>PostgreSQL</> 9.2 cluster
575    that uses a configuration-file-only directory, you must pass the
576    real data directory location to <application>pg_upgrade</>, and
577    pass the configuration directory location to the server, e.g.
578    <literal>-d /real-data-directory -o '-D /configuration-directory'</>.
579   </para>
580
581   <para>
582    If you want to use link mode and you do not want your old cluster
583    to be modified when the new cluster is started, make a copy of the
584    old cluster and upgrade that in link mode. To make a valid copy
585    of the old cluster, use <command>rsync</> to create a dirty
586    copy of the old cluster while the server is running, then shut down
587    the old server and run <command>rsync</> again to update the copy with any
588    changes to make it consistent.
589   </para>
590
591  </sect2>
592
593 </sect1>