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

NOTES

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

SEE ALSO

464       initdb(1), pg_ctl(1), pg_dump(1), postgres(1)
465
466
467
468PostgreSQL 12.6                      2021                        PG_UPGRADE(1)
Impressum