1PG_UPGRADE(1)            PostgreSQL 16.1 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 12.14 to 13.10 or from 14.9 to 15.5. It is
17       not required for minor version upgrades, e.g., from 12.7 to 12.8 or
18       from 14.1 to 14.5.
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       --copy
135           Copy files to the new cluster. This is the default. (See also
136           --link and --clone.)
137
138       -?
139       --help
140           show help, then exit
141

USAGE

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

NOTES

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

SEE ALSO

492       initdb(1), pg_ctl(1), pg_dump(1), postgres(1)
493
494
495
496PostgreSQL 16.1                      2023                        PG_UPGRADE(1)
Impressum