11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/backup.sgml,v 2.53 2004/12/13 18:05:07 petere Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/backup.sgml,v 2.54 2004/12/28 19:08:58 tgl Exp $
33-->
44<chapter id="backup">
55 <title>Backup and Restore</title>
66
77 <indexterm zone="backup"><primary>backup</></>
88
99 <para>
10- As everything that contains valuable data, <productname>PostgreSQL</>
10+ Aswith everything that contains valuable data, <productname>PostgreSQL</>
1111 databases should be backed up regularly. While the procedure is
1212 essentially simple, it is important to have a basic understanding of
1313 the underlying techniques and assumptions.
@@ -46,9 +46,9 @@ pg_dump <replaceable class="parameter">dbname</replaceable> > <replaceable cl
4646 client application (albeit a particularly clever one). This means
4747 that you can do this backup procedure from any remote host that has
4848 access to the database. But remember that <application>pg_dump</>
49- does not operate with special permissions. In particular,you must
49+ does not operate with special permissions. In particular,it must
5050 have read access to all tables that you want to back up, so in
51- practice you almost always have tobe a database superuser.
51+ practice you almost always have torun it as a database superuser.
5252 </para>
5353
5454 <para>
@@ -111,26 +111,25 @@ psql <replaceable class="parameter">dbname</replaceable> < <replaceable class
111111 command, you must create it yourself from <literal>template0</> before executing
112112 <application>psql</> (e.g., with <literal>createdb -T template0
113113 <replaceable class="parameter">dbname</></literal>).
114- <application>psql</> supportssimilar options to <application>pg_dump</>
114+ <application>psql</> supports options similar to <application>pg_dump</>
115115 for controlling the database server location and the user name. See
116- its reference page for more information.
116+ <xref linkend="app-psql">'s reference page for more information.
117117 </para>
118118
119119 <para>
120- If the objects in the original database were owned by different
121- users, then the dump will instruct <application>psql</> to connect
122- as each affected user in turn and then create the relevant
123- objects. This way the original ownership is preserved. This also
124- means, however, that all these users must already exist, and
125- furthermore that you must be allowed to connect as each of them.
126- It might therefore be necessary to temporarily relax the client
127- authentication settings.
120+ Not only must the target database already exist before starting to
121+ run the restore, but so must all the users who own objects in the
122+ dumped database or were granted permissions on the objects. If they
123+ do not, then the restore will fail to recreate the objects with the
124+ original ownership and/or permissions. (Sometimes this is what you want,
125+ but usually it is not.)
128126 </para>
129127
130128 <para>
131129 Once restored, it is wise to run <xref linkend="sql-analyze"
132130 endterm="sql-analyze-title"> on each database so the optimizer has
133- useful statistics. You can also run <command>vacuumdb -a -z</> to
131+ useful statistics. An easy way to do this is to run
132+ <command>vacuumdb -a -z</> to
134133 <command>VACUUM ANALYZE</> all databases; this is equivalent to
135134 running <command>VACUUM ANALYZE</command> manually.
136135 </para>
@@ -189,7 +188,7 @@ psql template1 < <replaceable class="parameter">infile</replaceable>
189188 </sect2>
190189
191190 <sect2 id="backup-dump-large">
192- <title>Large Databases </title>
191+ <title>Handling large databases </title>
193192
194193 <para>
195194 Since <productname>PostgreSQL</productname> allows tables larger
@@ -249,17 +248,19 @@ cat <replaceable class="parameter">filename</replaceable>* | psql <replaceable c
249248 <formalpara>
250249 <title>Use the custom dump format.</title>
251250 <para>
252- If <productname>PostgreSQL</productname> was built on a system with the <application>zlib</> compression library
253- installed, the custom dump format will compress data as it writes it
254- to the output file. For large databases, this will produce similar dump
255- sizes to using <command>gzip</command>, buthas the added advantage that the tables can be
256- restored selectively. The following command dumps a database using the
257- custom dump format:
251+ If <productname>PostgreSQL</productname> was built on a system with the
252+ <application>zlib</> compression library installed, the custom dump
253+ format will compress data as it writes it to the output file. This will
254+ produce dump file sizessimilar to using <command>gzip</command>, butit
255+ has the added advantage that tables can be restored selectively. The
256+ following command dumps a database using the custom dump format:
258257
259258<programlisting>
260259pg_dump -Fc <replaceable class="parameter">dbname</replaceable> > <replaceable class="parameter">filename</replaceable>
261260</programlisting>
262261
262+ A custom-format dump is not a script for <application>psql</>, but
263+ instead must be restored with <application>pg_restore</>.
263264 See the <xref linkend="app-pgdump"> and <xref
264265 linkend="app-pgrestore"> reference pages for details.
265266 </para>
@@ -276,7 +277,8 @@ pg_dump -Fc <replaceable class="parameter">dbname</replaceable> > <replaceable c
276277 object</primary><secondary>backup</secondary></indexterm> To dump
277278 large objects you must use either the custom or the tar output
278279 format, and use the <option>-b</> option in
279- <application>pg_dump</>. See the reference pages for details. The
280+ <application>pg_dump</>. See the <xref linkend="app-pgdump"> reference
281+ page for details. The
280282 directory <filename>contrib/pg_dumplo</> of the
281283 <productname>PostgreSQL</> source tree also contains a program
282284 that can dump large objects.
@@ -366,7 +368,9 @@ tar -cf backup.tar /usr/local/pgsql/data
366368 data files and WAL log on different disks) there may not be any way
367369 to obtain exactly-simultaneous frozen snapshots of all the volumes.
368370 Read your file system documentation very carefully before trusting
369- to the consistent-snapshot technique in such situations.
371+ to the consistent-snapshot technique in such situations. The safest
372+ approach is to shut down the database server for long enough to
373+ establish all the frozen snapshots.
370374 </para>
371375
372376 <para>
@@ -616,9 +620,12 @@ archive_command = 'test ! -f .../%f && cp %p .../%f'
616620 modifications made to the data in your <productname>PostgreSQL</> database
617621 it will not restore changes made to configuration files (that is,
618622 <filename>postgresql.conf</>, <filename>pg_hba.conf</> and
619- <filename>pg_ident.conf</>) after the initial base backup.
623+ <filename>pg_ident.conf</>), since those are edited manually rather
624+ than through SQL operations.
620625 You may wish to keep the configuration files in a location that will
621- be backed up by your regular file system backup procedures.
626+ be backed up by your regular file system backup procedures. See
627+ <xref linkend="runtime-config-file-locations"> for how to relocate the
628+ configuration files.
622629 </para>
623630 </sect2>
624631
@@ -930,8 +937,8 @@ restore_command = 'cp /mnt/server/archivedir/%f %p'
930937 in the command.
931938 </para>
932939 <para>
933- It is important for the command to return a zero exit statusonly
934- if it succeeds. The command <emphasis>will</> be asked for file
940+ It is important for the command to return a zero exit statusif and
941+ only if it succeeds. The command <emphasis>will</> be asked for file
935942 names that are not present in the archive; it must return nonzero
936943 when so asked. Examples:
937944<programlisting>
@@ -1083,7 +1090,7 @@ restore_command = 'copy /mnt/server/archivedir/%f "%p"' # Windows
10831090 that was current when the base backup was taken. If you want to recover
10841091 into some child timeline (that is, you want to return to some state that
10851092 was itself generated after a recovery attempt), you need to specify the
1086- target timeline in <filename>recovery.conf</>. You cannot recover into
1093+ target timelineID in <filename>recovery.conf</>. You cannot recover into
10871094 timelines that branched off earlier than the base backup.
10881095 </para>
10891096 </sect2>
@@ -1131,6 +1138,13 @@ restore_command = 'copy /mnt/server/archivedir/%f "%p"' # Windows
11311138 <secondary>compatibility</secondary>
11321139 </indexterm>
11331140
1141+ <para>
1142+ This section discusses how to migrate your database data from one
1143+ <productname>PostgreSQL</> release to a newer one.
1144+ The software installation procedure <foreignphrase>per se</> is not the
1145+ subject of this section; those details are in <xref linkend="installation">.
1146+ </para>
1147+
11341148 <para>
11351149 As a general rule, the internal data storage format is subject to
11361150 change between major releases of <productname>PostgreSQL</> (where
@@ -1140,17 +1154,21 @@ restore_command = 'copy /mnt/server/archivedir/%f "%p"' # Windows
11401154 storage formats. For example, releases 7.0.1, 7.1.2, and 7.2 are
11411155 not compatible, whereas 7.1.1 and 7.1.2 are. When you update
11421156 between compatible versions, you can simply replace the executables
1143- and reuse the data area on disk. Otherwise you need to <quote>back
1144- up</> your data and <quote>restore</> it on the new server, using
1145- <application>pg_dump</>. There are checks in place that prevent you
1146- from using a data area with an incompatible version of
1147- <productname>PostgreSQL</productname>, so no harm can be done by
1148- confusing these things. It is recommended that you use the
1149- <application>pg_dump</> program from the newer version of
1150- <productname>PostgreSQL</> to take advantage of any enhancements in
1151- <application>pg_dump</> that may have been made. The precise
1152- installation procedure is not the subject of this section; those
1153- details are in <xref linkend="installation">.
1157+ and reuse the data directory on disk. Otherwise you need to back
1158+ up your data and restore it on the new server. This has to be done
1159+ using <application>pg_dump</>; file system level backup methods
1160+ obviously won't work. There are checks in place that prevent you
1161+ from using a data directory with an incompatible version of
1162+ <productname>PostgreSQL</productname>, so no great harm can be done by
1163+ trying to start the wrong server version on a data directory.
1164+ </para>
1165+
1166+ <para>
1167+ It is recommended that you use the <application>pg_dump</> and
1168+ <application>pg_dumpall</> programs from the newer version of
1169+ <productname>PostgreSQL</>, to take advantage of any enhancements
1170+ that may have been made in these programs. Current releases of the
1171+ dump programs can read data from any server version back to 7.0.
11541172 </para>
11551173
11561174 <para>
@@ -1165,11 +1183,17 @@ pg_dumpall -p 5432 | psql -d template1 -p 6543
11651183 to transfer your data. Or use an intermediate file if you want.
11661184 Then you can shut down the old server and start the new server at
11671185 the port the old one was running at. You should make sure that the
1168- database is not updated after you run <application>pg_dumpall</>,
1186+ old database is not updated after you run <application>pg_dumpall</>,
11691187 otherwise you will obviously lose that data. See <xref
11701188 linkend="client-authentication"> for information on how to prohibit
1171- access. In practice you probably want to test your client
1172- applications on the new setup before switching over.
1189+ access.
1190+ </para>
1191+
1192+ <para>
1193+ In practice you probably want to test your client
1194+ applications on the new setup before switching over completely.
1195+ This is another reason for setting up concurrent installations
1196+ of old and new versions.
11731197 </para>
11741198
11751199 <para>
@@ -1194,22 +1218,16 @@ psql template1 < backup
11941218 you of strategic places to perform these steps.
11951219 </para>
11961220
1197- <para>
1198- You will always need a SQL dump (<application>pg_dump</> dump) for
1199- migrating to a new release. File-system-level backups (including
1200- on-line backups) will not work, for the same reason that you can't
1201- just do the update in-place: the file formats won't necessarily be
1202- compatible across major releases.
1203- </para>
1204-
12051221 <note>
12061222 <para>
12071223 When you <quote>move the old installation out of the way</quote>
1208- itis no longer perfectly usable. Someparts of theinstallation
1209- containinformation about where the other parts are located. This
1210- is usually not a big problem but if you plan on using two
1224+ itmay no longerbe perfectly usable. Some of theexecutable programs
1225+ containabsolute paths to various installed programs and data files.
1226+ This is usually not a big problem but if you plan on using two
12111227 installations in parallel for a while you should assign them
1212- different installation directories at build time.
1228+ different installation directories at build time. (This problem
1229+ is rectified in <productname>PostgreSQL</> 8.0 and later, but you
1230+ need to be wary of moving older installations.)
12131231 </para>
12141232 </note>
12151233 </sect1>