1PG_UPGRADE(1)            PostgreSQL 15.4 Documentation           PG_UPGRADE(1)
2
3
4

NAME

6       pg_upgrade - upgrade a PostgreSQL server instance
7

SYNOPSIS

9       pg_upgrade -b oldbindir [-B newbindir] -d oldconfigdir -D newconfigdir
10                  [option...]
11

DESCRIPTION

13       pg_upgrade (formerly called pg_migrator) allows data stored in
14       PostgreSQL data files to be upgraded to a later PostgreSQL major
15       version without the data dump/restore typically required for major
16       version upgrades, e.g., from 9.5.8 to 9.6.4 or from 10.7 to 11.2. It is
17       not required for minor version upgrades, e.g., from 9.6.2 to 9.6.3 or
18       from 10.1 to 10.2.
19
20       Major PostgreSQL releases regularly add new features that often change
21       the layout of the system tables, but the internal data storage format
22       rarely changes.  pg_upgrade uses this fact to perform rapid upgrades by
23       creating new system tables and simply reusing the old user data files.
24       If a future major release ever changes the data storage format in a way
25       that makes the old data format unreadable, pg_upgrade will not be
26       usable for such upgrades. (The community will attempt to avoid such
27       situations.)
28
29       pg_upgrade does its best to make sure the old and new clusters are
30       binary-compatible, e.g., by checking for compatible compile-time
31       settings, including 32/64-bit binaries. It is important that any
32       external modules are also binary compatible, though this cannot be
33       checked by pg_upgrade.
34
35       pg_upgrade supports upgrades from 9.2.X and later to the current major
36       release of PostgreSQL, including snapshot and beta releases.
37

OPTIONS

39       pg_upgrade accepts the following command-line arguments:
40
41       -b bindir
42       --old-bindir=bindir
43           the old PostgreSQL executable directory; environment variable
44           PGBINOLD
45
46       -B bindir
47       --new-bindir=bindir
48           the new PostgreSQL executable directory; default is the directory
49           where pg_upgrade resides; environment variable PGBINNEW
50
51       -c
52       --check
53           check clusters only, don't change any data
54
55       -d configdir
56       --old-datadir=configdir
57           the old database cluster configuration directory; environment
58           variable PGDATAOLD
59
60       -D configdir
61       --new-datadir=configdir
62           the new database cluster configuration directory; environment
63           variable PGDATANEW
64
65       -j njobs
66       --jobs=njobs
67           number of simultaneous processes or threads to use
68
69       -k
70       --link
71           use hard links instead of copying files to the new cluster
72
73       -N
74       --no-sync
75           By default, pg_upgrade will wait for all files of the upgraded
76           cluster to be written safely to disk. This option causes pg_upgrade
77           to return without waiting, which is faster, but means that a
78           subsequent operating system crash can leave the data directory
79           corrupt. Generally, this option is useful for testing but should
80           not be used on a production installation.
81
82       -o options
83       --old-options options
84           options to be passed directly to the old postgres command; multiple
85           option invocations are appended
86
87       -O options
88       --new-options options
89           options to be passed directly to the new postgres command; multiple
90           option invocations are appended
91
92       -p port
93       --old-port=port
94           the old cluster port number; environment variable PGPORTOLD
95
96       -P port
97       --new-port=port
98           the new cluster port number; environment variable PGPORTNEW
99
100       -r
101       --retain
102           retain SQL and log files even after successful completion
103
104       -s dir
105       --socketdir=dir
106           directory to use for postmaster sockets during upgrade; default is
107           current working directory; environment variable PGSOCKETDIR
108
109       -U username
110       --username=username
111           cluster's install user name; environment variable PGUSER
112
113       -v
114       --verbose
115           enable verbose internal logging
116
117       -V
118       --version
119           display version information, then exit
120
121       --clone
122           Use efficient file cloning (also known as “reflinks” on some
123           systems) instead of copying files to the new cluster. This can
124           result in near-instantaneous copying of the data files, giving the
125           speed advantages of -k/--link while leaving the old cluster
126           untouched.
127
128           File cloning is only supported on some operating systems and file
129           systems. If it is selected but not supported, the pg_upgrade run
130           will error. At present, it is supported on Linux (kernel 4.5 or
131           later) with Btrfs and XFS (on file systems created with reflink
132           support), and on macOS with APFS.
133
134       -?
135       --help
136           show help, then exit
137

USAGE

139       These are the steps to perform an upgrade with pg_upgrade:
140
141        1. Optionally move the old cluster: If you are using a
142           version-specific installation directory, e.g., /opt/PostgreSQL/15,
143           you do not need to move the old cluster. The graphical installers
144           all use version-specific installation directories.
145
146           If your installation directory is not version-specific, e.g.,
147           /usr/local/pgsql, it is necessary to move the current PostgreSQL
148           install directory so it does not interfere with the new PostgreSQL
149           installation. Once the current PostgreSQL server is shut down, it
150           is safe to rename the PostgreSQL installation directory; assuming
151           the old directory is /usr/local/pgsql, you can do:
152
153               mv /usr/local/pgsql /usr/local/pgsql.old
154
155           to rename the directory.
156
157        2. For source installs, build the new version: Build the new
158           PostgreSQL source with configure flags that are compatible with the
159           old cluster.  pg_upgrade will check pg_controldata to make sure all
160           settings are compatible before starting the upgrade.
161
162        3. Install the new PostgreSQL binaries: Install the new server's
163           binaries and support files.  pg_upgrade is included in a default
164           installation.
165
166           For source installs, if you wish to install the new server in a
167           custom location, use the prefix variable:
168
169               make prefix=/usr/local/pgsql.new install
170
171        4. Initialize the new PostgreSQL cluster: Initialize the new cluster
172           using initdb. Again, use compatible initdb flags that match the old
173           cluster. Many prebuilt installers do this step automatically. There
174           is no need to start the new cluster.
175
176        5. Install extension shared object files: Many extensions and custom
177           modules, whether from contrib or another source, use shared object
178           files (or DLLs), e.g., pgcrypto.so. If the old cluster used these,
179           shared object files matching the new server binary must be
180           installed in the new cluster, usually via operating system
181           commands. Do not load the schema definitions, e.g., CREATE
182           EXTENSION pgcrypto, because these will be duplicated from the old
183           cluster. If extension updates are available, pg_upgrade will report
184           this and create a script that can be run later to update them.
185
186        6. Copy custom full-text search files: Copy any custom full text
187           search files (dictionary, synonym, thesaurus, stop words) from the
188           old to the new cluster.
189
190        7. Adjust authentication: pg_upgrade will connect to the old and new
191           servers several times, so you might want to set authentication to
192           peer in pg_hba.conf or use a ~/.pgpass file (see Section 34.16).
193
194        8. Stop both servers: Make sure both database servers are stopped
195           using, on Unix, e.g.:
196
197               pg_ctl -D /opt/PostgreSQL/9.6 stop
198               pg_ctl -D /opt/PostgreSQL/15 stop
199
200           or on Windows, using the proper service names:
201
202               NET STOP postgresql-9.6
203               NET STOP postgresql-15
204
205           Streaming replication and log-shipping standby servers can remain
206           running until a later step.
207
208        9. Prepare for standby server upgrades: If you are upgrading standby
209           servers using methods outlined in section Step 11, verify that the
210           old standby servers are caught up by running pg_controldata against
211           the old primary and standby clusters. Verify that the “Latest
212           checkpoint location” values match in all clusters. (There will be a
213           mismatch if old standby servers were shut down before the old
214           primary or if the old standby servers are still running.) Also,
215           make sure wal_level is not set to minimal in the postgresql.conf
216           file on the new primary cluster.
217
218        10. Run pg_upgrade: Always run the pg_upgrade binary of the new
219           server, not the old one.  pg_upgrade requires the specification of
220           the old and new cluster's data and executable (bin) directories.
221           You can also specify user and port values, and whether you want the
222           data files linked or cloned instead of the default copy behavior.
223
224           If you use link mode, the upgrade will be much faster (no file
225           copying) and use less disk space, but you will not be able to
226           access your old cluster once you start the new cluster after the
227           upgrade. Link mode also requires that the old and new cluster data
228           directories be in the same file system. (Tablespaces and pg_wal can
229           be on different file systems.) Clone mode provides the same speed
230           and disk space advantages but does not cause the old cluster to be
231           unusable once the new cluster is started. Clone mode also requires
232           that the old and new data directories be in the same file system.
233           This mode is only available on certain operating systems and file
234           systems.
235
236           The --jobs option allows multiple CPU cores to be used for
237           copying/linking of files and to dump and restore database schemas
238           in parallel; a good place to start is the maximum of the number of
239           CPU cores and tablespaces. This option can dramatically reduce the
240           time to upgrade a multi-database server running on a multiprocessor
241           machine.
242
243           For Windows users, you must be logged into an administrative
244           account, and then start a shell as the postgres user and set the
245           proper path:
246
247               RUNAS /USER:postgres "CMD.EXE"
248               SET PATH=%PATH%;C:\Program Files\PostgreSQL\15\bin;
249
250           and then run pg_upgrade with quoted directories, e.g.:
251
252               pg_upgrade.exe
253                       --old-datadir "C:/Program Files/PostgreSQL/9.6/data"
254                       --new-datadir "C:/Program Files/PostgreSQL/15/data"
255                       --old-bindir "C:/Program Files/PostgreSQL/9.6/bin"
256                       --new-bindir "C:/Program Files/PostgreSQL/15/bin"
257
258           Once started, pg_upgrade will verify the two clusters are
259           compatible and then do the upgrade. You can use pg_upgrade --check
260           to perform only the checks, even if the old server is still
261           running.  pg_upgrade --check will also outline any manual
262           adjustments you will need to make after the upgrade. If you are
263           going to be using link or clone mode, you should use the option
264           --link or --clone with --check to enable mode-specific checks.
265           pg_upgrade requires write permission in the current directory.
266
267           Obviously, no one should be accessing the clusters during the
268           upgrade.  pg_upgrade defaults to running servers on port 50432 to
269           avoid unintended client connections. You can use the same port
270           number for both clusters when doing an upgrade because the old and
271           new clusters will not be running at the same time. However, when
272           checking an old running server, the old and new port numbers must
273           be different.
274
275           If an error occurs while restoring the database schema, pg_upgrade
276           will exit and you will have to revert to the old cluster as
277           outlined in Step 17 below. To try pg_upgrade again, you will need
278           to modify the old cluster so the pg_upgrade schema restore
279           succeeds. If the problem is a contrib module, you might need to
280           uninstall the contrib module from the old cluster and install it in
281           the new cluster after the upgrade, assuming the module is not being
282           used to store user data.
283
284        11. Upgrade streaming replication and log-shipping standby servers: If
285           you used link mode and have Streaming Replication (see
286           Section 27.2.5) or Log-Shipping (see Section 27.2) standby servers,
287           you can follow these steps to quickly upgrade them. You will not be
288           running pg_upgrade on the standby servers, but rather rsync on the
289           primary. Do not start any servers yet.
290
291           If you did not use link mode, do not have or do not want to use
292           rsync, or want an easier solution, skip the instructions in this
293           section and simply recreate the standby servers once pg_upgrade
294           completes and the new primary is running.
295
296            1. Install the new PostgreSQL binaries on standby servers: Make
297               sure the new binaries and support files are installed on all
298               standby servers.
299
300            2. Make sure the new standby data directories do not exist: Make
301               sure the new standby data directories do not exist or are
302               empty. If initdb was run, delete the standby servers' new data
303               directories.
304
305            3. Install extension shared object files: Install the same
306               extension shared object files on the new standbys that you
307               installed in the new primary cluster.
308
309            4. Stop standby servers: If the standby servers are still running,
310               stop them now using the above instructions.
311
312            5. Save configuration files: Save any configuration files from the
313               old standbys' configuration directories you need to keep, e.g.,
314               postgresql.conf (and any files included by it),
315               postgresql.auto.conf, pg_hba.conf, because these will be
316               overwritten or removed in the next step.
317
318            6. Run rsync: When using link mode, standby servers can be quickly
319               upgraded using rsync. To accomplish this, from a directory on
320               the primary server that is above the old and new database
321               cluster directories, run this on the primary for each standby
322               server:
323
324                   rsync --archive --delete --hard-links --size-only --no-inc-recursive old_cluster new_cluster remote_dir
325
326               where old_cluster and new_cluster are relative to the current
327               directory on the primary, and remote_dir is above the old and
328               new cluster directories on the standby. The directory structure
329               under the specified directories on the primary and standbys
330               must match. Consult the rsync manual page for details on
331               specifying the remote directory, e.g.,
332
333                   rsync --archive --delete --hard-links --size-only --no-inc-recursive /opt/PostgreSQL/9.5 \
334                         /opt/PostgreSQL/9.6 standby.example.com:/opt/PostgreSQL
335
336               You can verify what the command will do using rsync's --dry-run
337               option. While rsync must be run on the primary for at least one
338               standby, it is possible to run rsync on an upgraded standby to
339               upgrade other standbys, as long as the upgraded standby has not
340               been started.
341
342               What this does is to record the links created by pg_upgrade's
343               link mode that connect files in the old and new clusters on the
344               primary server. It then finds matching files in the standby's
345               old cluster and creates links for them in the standby's new
346               cluster. Files that were not linked on the primary are copied
347               from the primary to the standby. (They are usually small.) This
348               provides rapid standby upgrades. Unfortunately, rsync
349               needlessly copies files associated with temporary and unlogged
350               tables because these files don't normally exist on standby
351               servers.
352
353               If you have tablespaces, you will need to run a similar rsync
354               command for each tablespace directory, e.g.:
355
356                   rsync --archive --delete --hard-links --size-only --no-inc-recursive /vol1/pg_tblsp/PG_9.5_201510051 \
357                         /vol1/pg_tblsp/PG_9.6_201608131 standby.example.com:/vol1/pg_tblsp
358
359               If you have relocated pg_wal outside the data directories,
360               rsync must be run on those directories too.
361
362            7. Configure streaming replication and log-shipping standby
363               servers: Configure the servers for log shipping. (You do not
364               need to run pg_backup_start() and pg_backup_stop() or take a
365               file system backup as the standbys are still synchronized with
366               the primary.) Replication slots are not copied and must be
367               recreated.
368
369        12. Restore pg_hba.conf: If you modified pg_hba.conf, restore its
370           original settings. It might also be necessary to adjust other
371           configuration files in the new cluster to match the old cluster,
372           e.g., postgresql.conf (and any files included by it),
373           postgresql.auto.conf.
374
375        13. Start the new server: The new server can now be safely started,
376           and then any rsync'ed standby servers.
377
378        14. Post-upgrade processing: If any post-upgrade processing is
379           required, pg_upgrade will issue warnings as it completes. It will
380           also generate script files that must be run by the administrator.
381           The script files will connect to each database that needs
382           post-upgrade processing. Each script should be run using:
383
384               psql --username=postgres --file=script.sql postgres
385
386           The scripts can be run in any order and can be deleted once they
387           have been run.
388
389               Caution
390               In general it is unsafe to access tables referenced in rebuild
391               scripts until the rebuild scripts have run to completion; doing
392               so could yield incorrect results or poor performance. Tables
393               not referenced in rebuild scripts can be accessed immediately.
394
395        15. Statistics: Because optimizer statistics are not transferred by
396           pg_upgrade, you will be instructed to run a command to regenerate
397           that information at the end of the upgrade. You might need to set
398           connection parameters to match your new cluster.
399
400        16. Delete old cluster: Once you are satisfied with the upgrade, you
401           can delete the old cluster's data directories by running the script
402           mentioned when pg_upgrade completes. (Automatic deletion is not
403           possible if you have user-defined tablespaces inside the old data
404           directory.) You can also delete the old installation directories
405           (e.g., bin, share).
406
407        17. Reverting to old cluster: If, after running pg_upgrade, you wish
408           to revert to the old cluster, there are several options:
409
410           •   If the --check option was used, the old cluster was unmodified;
411               it can be restarted.
412
413           •   If the --link option was not used, the old cluster was
414               unmodified; it can be restarted.
415
416           •   If the --link option was used, the data files might be shared
417               between the old and new cluster:
418
419               •   If pg_upgrade aborted before linking started, the old
420                   cluster was unmodified; it can be restarted.
421
422               •   If you did not start the new cluster, the old cluster was
423                   unmodified except that, when linking started, a .old suffix
424                   was appended to $PGDATA/global/pg_control. To reuse the old
425                   cluster, remove the .old suffix from
426                   $PGDATA/global/pg_control; you can then restart the old
427                   cluster.
428
429               •   If you did start the new cluster, it has written to shared
430                   files and it is unsafe to use the old cluster. The old
431                   cluster will need to be restored from backup in this case.
432

NOTES

434       pg_upgrade creates various working files, such as schema dumps, stored
435       within pg_upgrade_output.d in the directory of the new cluster. Each
436       run creates a new subdirectory named with a timestamp formatted as per
437       ISO 8601 (%Y%m%dT%H%M%S), where all its generated files are stored.
438       pg_upgrade_output.d and its contained files will be removed
439       automatically if pg_upgrade completes successfully; but in the event of
440       trouble, the files there may provide useful debugging information.
441
442       pg_upgrade launches short-lived postmasters in the old and new data
443       directories. Temporary Unix socket files for communication with these
444       postmasters are, by default, made in the current working directory. In
445       some situations the path name for the current directory might be too
446       long to be a valid socket name. In that case you can use the -s option
447       to put the socket files in some directory with a shorter path name. For
448       security, be sure that that directory is not readable or writable by
449       any other users. (This is not supported on Windows.)
450
451       All failure, rebuild, and reindex cases will be reported by pg_upgrade
452       if they affect your installation; post-upgrade scripts to rebuild
453       tables and indexes will be generated automatically. If you are trying
454       to automate the upgrade of many clusters, you should find that clusters
455       with identical database schemas require the same post-upgrade steps for
456       all cluster upgrades; this is because the post-upgrade steps are based
457       on the database schemas, and not user data.
458
459       For deployment testing, create a schema-only copy of the old cluster,
460       insert dummy data, and upgrade that.
461
462       pg_upgrade does not support upgrading of databases containing table
463       columns using these reg* OID-referencing system data types:
464           regcollation
465           regconfig
466           regdictionary
467           regnamespace
468           regoper
469           regoperator
470           regproc
471           regprocedure
472       (regclass, regrole, and regtype can be upgraded.)
473
474       If you want to use link mode and you do not want your old cluster to be
475       modified when the new cluster is started, consider using the clone
476       mode. If that is not available, make a copy of the old cluster and
477       upgrade that in link mode. To make a valid copy of the old cluster, use
478       rsync to create a dirty copy of the old cluster while the server is
479       running, then shut down the old server and run rsync --checksum again
480       to update the copy with any changes to make it consistent. (--checksum
481       is necessary because rsync only has file modification-time granularity
482       of one second.) You might want to exclude some files, e.g.,
483       postmaster.pid, as documented in Section 26.3.3. If your file system
484       supports file system snapshots or copy-on-write file copies, you can
485       use that to make a backup of the old cluster and tablespaces, though
486       the snapshot and copies must be created simultaneously or while the
487       database server is down.
488

SEE ALSO

490       initdb(1), pg_ctl(1), pg_dump(1), postgres(1)
491
492
493
494PostgreSQL 15.4                      2023                        PG_UPGRADE(1)
Impressum