1PG_UPGRADE(1)            PostgreSQL 14.3 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/reload 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 8.4.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       -o options
74       --old-options options
75           options to be passed directly to the old postgres command; multiple
76           option invocations are appended
77
78       -O options
79       --new-options options
80           options to be passed directly to the new postgres command; multiple
81           option invocations are appended
82
83       -p port
84       --old-port=port
85           the old cluster port number; environment variable PGPORTOLD
86
87       -P port
88       --new-port=port
89           the new cluster port number; environment variable PGPORTNEW
90
91       -r
92       --retain
93           retain SQL and log files even after successful completion
94
95       -s dir
96       --socketdir=dir
97           directory to use for postmaster sockets during upgrade; default is
98           current working directory; environment variable PGSOCKETDIR
99
100       -U username
101       --username=username
102           cluster's install user name; environment variable PGUSER
103
104       -v
105       --verbose
106           enable verbose internal logging
107
108       -V
109       --version
110           display version information, then exit
111
112       --clone
113           Use efficient file cloning (also known as “reflinks” on some
114           systems) instead of copying files to the new cluster. This can
115           result in near-instantaneous copying of the data files, giving the
116           speed advantages of -k/--link while leaving the old cluster
117           untouched.
118
119           File cloning is only supported on some operating systems and file
120           systems. If it is selected but not supported, the pg_upgrade run
121           will error. At present, it is supported on Linux (kernel 4.5 or
122           later) with Btrfs and XFS (on file systems created with reflink
123           support), and on macOS with APFS.
124
125       -?
126       --help
127           show help, then exit
128

USAGE

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

NOTES

411       pg_upgrade creates various working files, such as schema dumps, in the
412       current working directory. For security, be sure that that directory is
413       not readable or writable by any other users.
414
415       pg_upgrade launches short-lived postmasters in the old and new data
416       directories. Temporary Unix socket files for communication with these
417       postmasters are, by default, made in the current working directory. In
418       some situations the path name for the current directory might be too
419       long to be a valid socket name. In that case you can use the -s option
420       to put the socket files in some directory with a shorter path name. For
421       security, be sure that that directory is not readable or writable by
422       any other users. (This is not supported on Windows.)
423
424       All failure, rebuild, and reindex cases will be reported by pg_upgrade
425       if they affect your installation; post-upgrade scripts to rebuild
426       tables and indexes will be generated automatically. If you are trying
427       to automate the upgrade of many clusters, you should find that clusters
428       with identical database schemas require the same post-upgrade steps for
429       all cluster upgrades; this is because the post-upgrade steps are based
430       on the database schemas, and not user data.
431
432       For deployment testing, create a schema-only copy of the old cluster,
433       insert dummy data, and upgrade that.
434
435       pg_upgrade does not support upgrading of databases containing table
436       columns using these reg* OID-referencing system data types:
437           regcollation
438           regconfig
439           regdictionary
440           regnamespace
441           regoper
442           regoperator
443           regproc
444           regprocedure
445       (regclass, regrole, and regtype can be upgraded.)
446
447       If you are upgrading a pre-PostgreSQL 9.2 cluster that uses a
448       configuration-file-only directory, you must pass the real data
449       directory location to pg_upgrade, and pass the configuration directory
450       location to the server, e.g., -d /real-data-directory -o '-D
451       /configuration-directory'.
452
453       If using a pre-9.1 old server that is using a non-default Unix-domain
454       socket directory or a default that differs from the default of the new
455       cluster, set PGHOST to point to the old server's socket location. (This
456       is not relevant on Windows.)
457
458       If you want to use link mode and you do not want your old cluster to be
459       modified when the new cluster is started, consider using the clone
460       mode. If that is not available, make a copy of the old cluster and
461       upgrade that in link mode. To make a valid copy of the old cluster, use
462       rsync to create a dirty copy of the old cluster while the server is
463       running, then shut down the old server and run rsync --checksum again
464       to update the copy with any changes to make it consistent. (--checksum
465       is necessary because rsync only has file modification-time granularity
466       of one second.) You might want to exclude some files, e.g.,
467       postmaster.pid, as documented in Section 26.3.3. If your file system
468       supports file system snapshots or copy-on-write file copies, you can
469       use that to make a backup of the old cluster and tablespaces, though
470       the snapshot and copies must be created simultaneously or while the
471       database server is down.
472

SEE ALSO

474       initdb(1), pg_ctl(1), pg_dump(1), postgres(1)
475
476
477
478PostgreSQL 14.3                      2022                        PG_UPGRADE(1)
Impressum