OSDN Git Service

In the pg_upgrade docs, move 50432 port mention to the place where we
[pg-rex/syncrep.git] / 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 session activity to file</para></listitem>
115      </varlistentry>
116
117      <varlistentry>
118       <term><option>-p</option> <replaceable>old_port_number</></term>
119       <term><option>--old-port=</option><replaceable>old_portnum</></term>
120       <listitem><para>the old cluster port number; environment
121       variable <envar>PGPORTOLD</></para></listitem>
122      </varlistentry>
123
124      <varlistentry>
125       <term><option>-P</option> <replaceable>new_port_number</></term>
126       <term><option>--new-port=</option><replaceable>new_portnum</></term>
127       <listitem><para>the new cluster port number; environment
128       variable <envar>PGPORTNEW</></para></listitem>
129      </varlistentry>
130
131      <varlistentry>
132       <term><option>-u</option> <replaceable>user_name</></term>
133       <term><option>--user=</option><replaceable>user_name</></term>
134       <listitem><para>cluster's super user name; environment
135       variable <envar>PGUSER</></para></listitem>
136      </varlistentry>
137
138      <varlistentry>
139       <term><option>-v</option></term>
140       <term><option>--verbose</option></term>
141       <listitem><para>enable verbose output</para></listitem>
142      </varlistentry>
143
144      <varlistentry>
145       <term><option>-V</option></term>
146       <term><option>--version</option></term>
147       <listitem><para>display version information, then exit</para></listitem>
148      </varlistentry>
149
150      <varlistentry>
151       <term><option>-?</option></term>
152       <term><option>-h</option></term>
153       <term><option>--help</option></term>
154       <listitem><para>show help, then exit</para></listitem>
155      </varlistentry>
156
157     </variablelist>
158    </para>
159
160  </sect2>
161
162  <sect2>
163   <title>Upgrade Steps</title>
164
165   <procedure>
166    <step performance="optional">
167     <title>Optionally move the old cluster</title>
168
169     <para>
170      If you are using a version-specific installation directory, e.g.
171      <filename>/opt/PostgreSQL/8.4</>, you do not need to move the old cluster. The
172      one-click installers all use version-specific installation directories.
173     </para>
174
175     <para>
176      If your installation directory is not version-specific, e.g.
177      <filename>/usr/local/pgsql</>, it is necessary to move the current PostgreSQL install
178      directory so it does not interfere with the new <productname>PostgreSQL</> installation.
179      Once the current <productname>PostgreSQL</> server is shut down, it is safe to rename the
180      PostgreSQL installation directory; assuming the old directory is
181      <filename>/usr/local/pgsql</>, you can do:
182
183 <programlisting>
184 mv /usr/local/pgsql /usr/local/pgsql.old
185 </programlisting>
186      to rename the directory.
187     </para>
188    </step>
189
190    <step>
191     <title>For source installs, build the new version</title>
192
193     <para>
194      Build the new PostgreSQL source with <command>configure</> flags that are compatible
195      with the old cluster. <application>pg_upgrade</> will check <command>pg_controldata</> to make
196      sure all settings are compatible before starting the upgrade.
197     </para>
198    </step>
199
200    <step>
201     <title>Install the new PostgreSQL binaries</title>
202
203     <para>
204      Install the new server's binaries and support files. You can use the
205      same port numbers for both clusters, typically 5432, because the old and
206      new clusters will not be running at the same time.
207     </para>
208
209     <para>
210      For source installs, if you wish to install the new server in a custom
211      location, use the <literal>prefix</literal> variable:
212
213 <programlisting>
214 gmake prefix=/usr/local/pgsql.new install
215 </programlisting>
216     </para>
217    </step>
218
219    <step>
220     <title>Install pg_upgrade and pg_upgrade_support</title>
221
222     <para>
223      Install the <application>pg_upgrade</> binary and
224      <application>pg_upgrade_support</> library in the new PostgreSQL cluster.
225     </para>
226    </step>
227
228    <step>
229     <title>Initialize the new PostgreSQL cluster</title>
230
231     <para>
232      Initialize the new cluster using <command>initdb</command>.
233      Again, use compatible <command>initdb</command>
234      flags that match the old cluster. Many
235      prebuilt installers do this step automatically. There is no need to
236      start the new cluster.
237     </para>
238    </step>
239
240    <step>
241     <title>Install custom shared object files</title>
242
243     <para>
244      Install any custom shared object files (or DLLs) used by the old cluster
245      into the new cluster, e.g. <filename>pgcrypto.so</filename>, whether they are from <filename>contrib</filename>
246      or some other source. Do not install the schema definitions, e.g.
247      <filename>pgcrypto.sql</>, because these will be upgraded from the old cluster.
248     </para>
249    </step>
250
251    <step>
252     <title>Adjust authentication</title>
253
254     <para>
255      <command>pg_upgrade</> will connect to the old and new servers several times,
256      so you might want to set authentication to <literal>trust</> in
257      <filename>pg_hba.conf</>, or if using <literal>md5</> authentication,
258      use a <filename>~/.pgpass</> file (see <xref linkend="libpq-pgpass">)
259      to avoid being prompted repeatedly for a password.
260     </para>
261    </step>
262
263    <step>
264     <title>Stop both servers</title>
265
266     <para>
267      Make sure both database servers are stopped using, on Unix, e.g.:
268
269 <programlisting>
270 pg_ctl -D /opt/PostgreSQL/8.4 stop
271 pg_ctl -D /opt/PostgreSQL/9.0 stop
272 </programlisting>
273
274      or on Windows, using the proper service names:
275
276 <programlisting>
277 NET STOP postgresql-8.4
278 NET STOP postgresql-9.0
279 </programlisting>
280
281      or
282
283 <programlisting>
284 NET STOP pgsql-8.3  (<productname>PostgreSQL</> 8.3 and older used a different service name)
285 </programlisting>
286     </para>
287    </step>
288
289    <step>
290     <title>Run <application>pg_upgrade</></title>
291
292     <para>
293      Always run the <application>pg_upgrade</> binary of the new server, not the old one.
294      <application>pg_upgrade</> requires the specification of the old and new cluster's
295      data and executable (<filename>bin</>) directories. You can also specify
296      user and port values, and whether you want the data linked instead of
297      copied (the default). 
298     </para>
299
300     <para>
301      If you use link mode, the upgrade will be much faster (no file
302      copying), but you will not be able to access your old cluster
303      once you start the new cluster after the upgrade.  Link mode also
304      requires that the old and new cluster data directories be in the
305      same file system.  See <literal>pg_upgrade --help</> for a full
306      list of options.
307    </para>
308
309    <para>
310    </para>
311
312     <para>
313      For Windows users, you must be logged into an administrative account, and
314      then start a shell as the <literal>postgres</> user and set the proper path:
315
316 <programlisting>
317 RUNAS /USER:postgres "CMD.EXE"
318 SET PATH=%PATH%;C:\Program Files\PostgreSQL\9.0\bin;
319 </programlisting>
320
321      and then run <application>pg_upgrade</> with quoted directories, e.g.:
322
323 <programlisting>
324 pg_upgrade.exe
325         --old-datadir "C:/Program Files/PostgreSQL/8.4/data"
326         --new-datadir "C:/Program Files/PostgreSQL/9.0/data"
327         --old-bindir "C:/Program Files/PostgreSQL/8.4/bin"
328         --new-bindir "C:/Program Files/PostgreSQL/9.0/bin"
329 </programlisting>
330
331      Once started, <command>pg_upgrade</> will verify the two clusters are compatible
332      and then do the upgrade. You can use <command>pg_upgrade --check</>
333      to perform only the checks, even if the old server is still
334      running. <command>pg_upgrade --check</> will also outline any
335      manual adjustments you will need to make after the upgrade.
336      <command>pg_upgrade</> requires write permission in the current directory.
337     </para>
338
339     <para>
340      Obviously, no one should be accessing the clusters during the
341      upgrade.  <application>pg_upgrade</> defaults to running servers
342      on port 50432 to avoid unintended client connections.
343     </para>
344
345     <para>
346      If an error occurs while restoring the database schema, <command>pg_upgrade</> will
347      exit and you will have to revert to the old cluster as outlined in <xref linkend="pgupgrade-step-revert">
348      below. To try <command>pg_upgrade</command> again, you will need to modify the old
349      cluster so the pg_upgrade schema restore succeeds. If the problem is a
350      contrib module, you might need to uninstall the contrib module from
351      the old cluster and install it in the new cluster after the upgrade,
352      assuming the module is not being used to store user data.
353     </para>
354    </step>
355
356    <step>
357     <title>Restore <filename>pg_hba.conf</></title>
358
359     <para>
360      If you modified <filename>pg_hba.conf</> to use <literal>trust</>,
361      restore its original authentication settings.
362     </para>
363    </step>
364
365    <step>
366     <title>Post-Upgrade processing</title>
367
368     <para>
369      If any post-upgrade processing is required, pg_upgrade will issue
370      warnings as it completes. It will also generate script files that must
371      be run by the administrator. The script files will connect to each
372      database that needs post-upgrade processing. Each script should be
373      run using:
374
375 <programlisting>
376 psql --username postgres --file script.sql postgres
377 </programlisting>
378
379      The scripts can be run in any order and can be deleted once they have
380      been run.
381     </para>
382
383     <caution>
384     <para>
385      In general it is unsafe to access tables referenced in rebuild scripts
386      until the rebuild scripts have run to completion; doing so could yield
387      incorrect results or poor performance. Tables not referenced in rebuild
388      scripts can be accessed immediately.
389     </para>
390     </caution>
391    </step>
392
393    <step>
394     <title>Statistics</title>
395
396     <para>
397      Because optimizer statistics are not transferred by <command>pg_upgrade</>, you will
398      be instructed to run a command to regenerate that information at the end
399      of the upgrade.
400     </para>
401    </step>
402
403    <step>
404     <title>Delete old cluster</title>
405
406     <para>
407      Once you are satisfied with the upgrade, you can delete the old
408      cluster's data directories by running the script mentioned when
409      <command>pg_upgrade</command> completes. You can also delete the
410      old installation directories
411      (e.g. <filename>bin</>, <filename>share</>).
412     </para>
413    </step>
414
415    <step id="pgupgrade-step-revert" performance="optional">
416     <title>Reverting to old cluster</title>
417
418     <para>
419      If, after running <command>pg_upgrade</command>, you wish to revert to the old cluster,
420      there are several options:
421
422      <itemizedlist>
423       <listitem>
424        <para>
425         If you ran <command>pg_upgrade</command>
426         with <option>--check</>, no modifications were made to the old
427         cluster and you can re-use it anytime.
428        </para>
429       </listitem>
430
431       <listitem>
432        <para>
433         If you ran <command>pg_upgrade</command>
434         with <option>--link</>, the data files are shared between the
435         old and new cluster. If you started the new cluster, the new
436         server has written to those shared files and it is unsafe to
437         use the old cluster.
438        </para>
439       </listitem>
440
441       <listitem>
442        <para>
443         If you
444         ran <command>pg_upgrade</command> <emphasis>without</> <option>--link</>
445         or did not start the new server, the old cluster was not
446         modified except that an <literal>.old</> suffix was appended
447         to <filename>$PGDATA/global/pg_control</> and perhaps
448         tablespace directories. To reuse the old cluster, remove
449         the <filename>.old</> suffix
450         from <filename>$PGDATA/global/pg_control</>. and, if upgrading
451         to 8.4 or earlier, remove the tablespace directories created
452         by the upgrade and remove the <filename>.old</> suffix from
453         the tablespace directory names; then you can restart the old
454         cluster.
455        </para>
456       </listitem>
457      </itemizedlist>
458     </para>
459    </step>
460   </procedure>
461
462  </sect2>
463
464  <sect2>
465   <title>Limitations in Upgrading <emphasis>from</> PostgreSQL 8.3</title>
466
467   <para>
468    Upgrading from PostgreSQL 8.3 has additional restrictions not present
469    when upgrading from later PostgreSQL releases.  For example,
470    pg_upgrade will not work for upgrading from 8.3 if a user column
471    is defined as:
472    <itemizedlist>
473     <listitem>
474      <para>
475       a <type>tsquery</> data type
476      </para>
477     </listitem>
478     <listitem>
479      <para>
480       data type <type>name</> and is not the first column
481      </para>
482     </listitem>
483    </itemizedlist>
484   </para>
485
486   <para>
487    You must drop any such columns and upgrade them manually.
488   </para>
489
490   <para>
491    pg_upgrade will require a table rebuild if:
492    <itemizedlist>
493     <listitem>
494      <para>
495       a user column is of data type <type>tsvector</type>
496      </para>
497     </listitem>
498    </itemizedlist>
499   </para>
500
501   <para>
502    pg_upgrade will require a reindex if:
503    <itemizedlist>
504     <listitem>
505      <para>
506       an index is of type hash or GIN
507      </para>
508     </listitem>
509     <listitem>
510      <para>
511       an index uses <function>bpchar_pattern_ops</>
512      </para>
513     </listitem>
514    </itemizedlist>
515   </para>
516
517   <para>
518    Also, the default datetime storage format changed to integer after
519    <productname>PostgreSQL</> 8.3. pg_upgrade will check that the datetime storage format
520    used by the old and new clusters match. Make sure your new cluster is
521    built with the configure flag <option>--disable-integer-datetimes</>.
522   </para>
523
524   <para>
525    For Windows users, note that due to different integer datetimes settings
526    used by the one-click installer and the MSI installer, it is only
527    possible to upgrade from version 8.3 of the one-click distribution to
528    version 8.4 or later of the one-click distribution. It is not
529    possible to upgrade from the MSI installer to the one-click installer.
530   </para>
531
532  </sect2>
533
534  <sect2>
535   <title>Notes</title>
536
537   <para>
538    <application>pg_upgrade</> does not support upgrading of databases
539    containing these <type>reg*</> OID-referencing system data types:
540    <type>regproc</>, <type>regprocedure</>, <type>regoper</>,
541    <type>regoperator</>, <type>regclass</>, <type>regconfig</>, and
542    <type>regdictionary</>.  (<type>regtype</> can be upgraded.)
543   </para>
544
545   <para>
546    All failure, rebuild, and reindex cases will be reported by
547    <application>pg_upgrade</> if they affect your installation;
548    post-upgrade scripts to rebuild tables and indexes will be
549    generated automatically.
550   </para>
551
552   <para>
553    For deployment testing, create a schema-only copy of the old cluster,
554    insert dummy data, and upgrade that.
555   </para>
556
557   <para>
558    If you want to use link mode and you don't want your old cluster
559    to be modified when the new cluster is started, make a copy of the
560    old cluster and upgrade that with link mode. To make a valid copy
561    of the old cluster, use <command>rsync</> to create a dirty
562    copy of the old cluster while the server is running, then shut down
563    the old server and run <command>rsync</> again to update the copy with any
564    changes to make it consistent.
565   </para>
566
567  </sect2>
568
569 </sect1>