]> begriffs open source - ai-pg/blob - full-docs/txt/pgupgrade.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / pgupgrade.txt
1
2 pg_upgrade
3
4    pg_upgrade — upgrade a PostgreSQL server instance
5
6 Synopsis
7
8    pg_upgrade -b oldbindir [-B newbindir] -d oldconfigdir -D newconfigdir
9    [option...]
10
11 Description
12
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
38 Warning
39
40    Upgrading a cluster causes the destination to execute arbitrary code of
41    the source superusers' choice. Ensure that the source superusers are
42    trusted before upgrading.
43
44 Options
45
46    pg_upgrade accepts the following command-line arguments:
47
48    -b bindir
49           --old-bindir=bindir
50           the old PostgreSQL executable directory; environment variable
51           PGBINOLD
52
53    -B bindir
54           --new-bindir=bindir
55           the new PostgreSQL executable directory; default is the
56           directory where pg_upgrade resides; environment variable
57           PGBINNEW
58
59    -c
60           --check
61           check clusters only, don't change any data
62
63    -d configdir
64           --old-datadir=configdir
65           the old database cluster configuration directory; environment
66           variable PGDATAOLD
67
68    -D configdir
69           --new-datadir=configdir
70           the new database cluster configuration directory; environment
71           variable PGDATANEW
72
73    -j njobs
74           --jobs=njobs
75           number of simultaneous connections and processes/threads to use
76
77    -k
78           --link
79           use hard links instead of copying files to the new cluster
80
81    -N
82           --no-sync
83           By default, pg_upgrade will wait for all files of the upgraded
84           cluster to be written safely to disk. This option causes
85           pg_upgrade to return without waiting, which is faster, but means
86           that a subsequent operating system crash can leave the data
87           directory corrupt. Generally, this option is useful for testing
88           but should not be used on a production installation.
89
90    -o options
91           --old-options options
92           options to be passed directly to the old postgres command;
93           multiple option invocations are appended
94
95    -O options
96           --new-options options
97           options to be passed directly to the new postgres command;
98           multiple option invocations are appended
99
100    -p port
101           --old-port=port
102           the old cluster port number; environment variable PGPORTOLD
103
104    -P port
105           --new-port=port
106           the new cluster port number; environment variable PGPORTNEW
107
108    -r
109           --retain
110           retain SQL and log files even after successful completion
111
112    -s dir
113           --socketdir=dir
114           directory to use for postmaster sockets during upgrade; default
115           is current working directory; environment variable PGSOCKETDIR
116
117    -U username
118           --username=username
119           cluster's install user name; environment variable PGUSER
120
121    -v
122           --verbose
123           enable verbose internal logging
124
125    -V
126           --version
127           display version information, then exit
128
129    --clone
130           Use efficient file cloning (also known as “reflinks” on some
131           systems) instead of copying files to the new cluster. This can
132           result in near-instantaneous copying of the data files, giving
133           the speed advantages of -k/--link while leaving the old cluster
134           untouched.
135
136           File cloning is only supported on some operating systems and
137           file systems. If it is selected but not supported, the
138           pg_upgrade run will error. At present, it is supported on Linux
139           (kernel 4.5 or later) with Btrfs and XFS (on file systems
140           created with reflink support), and on macOS with APFS.
141
142    --copy
143           Copy files to the new cluster. This is the default. (See also
144           --link, --clone, --copy-file-range, and --swap.)
145
146    --copy-file-range
147           Use the copy_file_range system call for efficient copying. On
148           some file systems this gives results similar to --clone, sharing
149           physical disk blocks, while on others it may still copy blocks,
150           but do so via an optimized path. At present, it is supported on
151           Linux and FreeBSD.
152
153    --no-statistics
154           Do not restore statistics from the old cluster into the new
155           cluster.
156
157    --set-char-signedness=option
158           Manually set the default char signedness of new clusters.
159           Possible values are signed and unsigned.
160
161           In the C language, the default signedness of the char type (when
162           not explicitly specified) varies across platforms. For example,
163           char defaults to signed char on x86 CPUs but to unsigned char on
164           ARM CPUs.
165
166           Starting from PostgreSQL 18, database clusters maintain their
167           own default char signedness setting, which can be used to ensure
168           consistent behavior across platforms with different default char
169           signedness. By default, pg_upgrade preserves the char signedness
170           setting when upgrading from an existing cluster. However, when
171           upgrading from PostgreSQL 17 or earlier, pg_upgrade adopts the
172           char signedness of the platform on which it was built.
173
174           This option allows you to explicitly set the default char
175           signedness for the new cluster, overriding any inherited values.
176           There are two specific scenarios where this option is relevant:
177
178           + If you are planning to migrate to a different platform after
179             the upgrade, you should not use this option. The default
180             behavior is right in this case. Instead, perform the upgrade
181             on the original platform without this flag, and then migrate
182             the cluster afterward. This is the recommended and safest
183             approach.
184           + If you have already migrated the cluster to a platform with
185             different char signedness (for example, from an x86-based
186             system to an ARM-based system), you should use this option to
187             specify the signedness matching the original platform's
188             default char signedness. Additionally, it's essential not to
189             modify any data files between migrating data files and running
190             pg_upgrade. pg_upgrade should be the first operation that
191             starts the cluster on the new platform.
192
193    --swap
194           Move the data directories from the old cluster to the new
195           cluster. Then, replace the catalog files with those generated
196           for the new cluster. This mode can outperform --link, --clone,
197           --copy, and --copy-file-range, especially on clusters with many
198           relations.
199
200           However, this mode creates many garbage files in the old
201           cluster, which can prolong the file synchronization step if
202           --sync-method=syncfs is used. Therefore, it is recommended to
203           use --sync-method=fsync with --swap.
204
205           Additionally, once the file transfer step begins, the old
206           cluster will be destructively modified and therefore will no
207           longer be safe to start. See Step 17 for details.
208
209    --sync-method=method
210           When set to fsync, which is the default, pg_upgrade will
211           recursively open and synchronize all files in the upgraded
212           cluster's data directory. The search for files will follow
213           symbolic links for the WAL directory and each configured
214           tablespace.
215
216           On Linux, syncfs may be used instead to ask the operating system
217           to synchronize the whole file systems that contain the upgraded
218           cluster's data directory, its WAL files, and each tablespace.
219           See recovery_init_sync_method for information about the caveats
220           to be aware of when using syncfs.
221
222           This option has no effect when --no-sync is used.
223
224    -?
225           --help
226           show help, then exit
227
228 Usage
229
230    These are the steps to perform an upgrade with pg_upgrade:
231
232 Note
233
234    The steps to upgrade logical replication clusters are not covered here;
235    refer to Section 29.13 for details.
236     1. Optionally move the old cluster
237        If you are using a version-specific installation directory, e.g.,
238        /opt/PostgreSQL/18, you do not need to move the old cluster. The
239        graphical installers all use version-specific installation
240        directories.
241        If your installation directory is not version-specific, e.g.,
242        /usr/local/pgsql, it is necessary to move the current PostgreSQL
243        install directory so it does not interfere with the new PostgreSQL
244        installation. Once the current PostgreSQL server is shut down, it
245        is safe to rename the PostgreSQL installation directory; assuming
246        the old directory is /usr/local/pgsql, you can do:
247 mv /usr/local/pgsql /usr/local/pgsql.old
248
249        to rename the directory.
250     2. For source installs, build the new version
251        Build the new PostgreSQL source with configure flags that are
252        compatible with the old cluster. pg_upgrade will check
253        pg_controldata to make sure all settings are compatible before
254        starting the upgrade.
255     3. Install the new PostgreSQL binaries
256        Install the new server's binaries and support files. pg_upgrade is
257        included in a default installation.
258        For source installs, if you wish to install the new server in a
259        custom location, use the prefix variable:
260 make prefix=/usr/local/pgsql.new install
261
262     4. Initialize the new PostgreSQL cluster
263        Initialize the new cluster using initdb. Again, use compatible
264        initdb flags that match the old cluster. Many prebuilt installers
265        do this step automatically. There is no need to start the new
266        cluster.
267     5. Install extension shared object files
268        Many extensions and custom modules, whether from contrib or another
269        source, use shared object files (or DLLs), e.g., pgcrypto.so. If
270        the old cluster used these, shared object files matching the new
271        server binary must be installed in the new cluster, usually via
272        operating system commands. Do not load the schema definitions,
273        e.g., CREATE EXTENSION pgcrypto, because these will be duplicated
274        from the old cluster. If extension updates are available,
275        pg_upgrade will report this and create a script that can be run
276        later to update them.
277     6. Copy custom full-text search files
278        Copy any custom full text search files (dictionary, synonym,
279        thesaurus, stop words) from the old to the new cluster.
280     7. Adjust authentication
281        pg_upgrade will connect to the old and new servers several times,
282        so you might want to set authentication to peer in pg_hba.conf or
283        use a ~/.pgpass file (see Section 32.16).
284     8. Stop both servers
285        Make sure both database servers are stopped using, on Unix, e.g.:
286 pg_ctl -D /opt/PostgreSQL/12 stop
287 pg_ctl -D /opt/PostgreSQL/18 stop
288
289        or on Windows, using the proper service names:
290 NET STOP postgresql-12
291 NET STOP postgresql-18
292
293        Streaming replication and log-shipping standby servers must be
294        running during this shutdown so they receive all changes.
295     9. Prepare for standby server upgrades
296        If you are upgrading standby servers using methods outlined in
297        section Step 11, verify that the old standby servers are caught up
298        by running pg_controldata against the old primary and standby
299        clusters. Verify that the “Latest checkpoint location” values match
300        in all clusters. Also, make sure wal_level is not set to minimal in
301        the postgresql.conf file on the new primary cluster.
302    10. Run pg_upgrade
303        Always run the pg_upgrade binary of the new server, not the old
304        one. pg_upgrade requires the specification of the old and new
305        cluster's data and executable (bin) directories. You can also
306        specify user and port values, and whether you want the data files
307        linked, cloned, or swapped instead of the default copy behavior.
308        If you use link mode, the upgrade will be much faster (no file
309        copying) and use less disk space, but you will not be able to
310        access your old cluster once you start the new cluster after the
311        upgrade. Link mode also requires that the old and new cluster data
312        directories be in the same file system. (Tablespaces and pg_wal can
313        be on different file systems.) Clone mode provides the same speed
314        and disk space advantages but does not cause the old cluster to be
315        unusable once the new cluster is started. Clone mode also requires
316        that the old and new data directories be in the same file system.
317        This mode is only available on certain operating systems and file
318        systems. Swap mode may be the fastest if there are many relations,
319        but you will not be able to access your old cluster once the file
320        transfer step begins. Swap mode also requires that the old and new
321        cluster data directories be in the same file system.
322        Setting --jobs to 2 or higher allows pg_upgrade to process multiple
323        databases and tablespaces in parallel. A good starting point is the
324        number of CPU cores on the machine. This option can substantially
325        reduce the upgrade time for multi-database and multi-tablespace
326        servers.
327        For Windows users, you must be logged into an administrative
328        account, and then run pg_upgrade with quoted directories, e.g.:
329 pg_upgrade.exe
330         --old-datadir "C:/Program Files/PostgreSQL/12/data"
331         --new-datadir "C:/Program Files/PostgreSQL/18/data"
332         --old-bindir "C:/Program Files/PostgreSQL/12/bin"
333         --new-bindir "C:/Program Files/PostgreSQL/18/bin"
334
335        Once started, pg_upgrade will verify the two clusters are
336        compatible and then do the upgrade. You can use pg_upgrade --check
337        to perform only the checks, even if the old server is still
338        running. pg_upgrade --check will also outline any manual
339        adjustments you will need to make after the upgrade. If you are
340        going to be using link, clone, copy-file-range, or swap mode, you
341        should use the option --link, --clone, --copy-file-range, or --swap
342        with --check to enable mode-specific checks. pg_upgrade requires
343        write permission in the current directory.
344        Obviously, no one should be accessing the clusters during the
345        upgrade. pg_upgrade defaults to running servers on port 50432 to
346        avoid unintended client connections. You can use the same port
347        number for both clusters when doing an upgrade because the old and
348        new clusters will not be running at the same time. However, when
349        checking an old running server, the old and new port numbers must
350        be different.
351        If an error occurs while restoring the database schema, pg_upgrade
352        will exit and you will have to revert to the old cluster as
353        outlined in Step 17 below. To try pg_upgrade again, you will need
354        to modify the old cluster so the pg_upgrade schema restore
355        succeeds. If the problem is a contrib module, you might need to
356        uninstall the contrib module from the old cluster and install it in
357        the new cluster after the upgrade, assuming the module is not being
358        used to store user data.
359    11. Upgrade streaming replication and log-shipping standby servers
360        If you used link mode and have Streaming Replication (see
361        Section 26.2.5) or Log-Shipping (see Section 26.2) standby servers,
362        you can follow these steps to quickly upgrade them. You will not be
363        running pg_upgrade on the standby servers, but rather rsync on the
364        primary. Do not start any servers yet.
365        If you did not use link mode, do not have or do not want to use
366        rsync, or want an easier solution, skip the instructions in this
367        section and simply recreate the standby servers once pg_upgrade
368        completes and the new primary is running.
369          1. Install the new PostgreSQL binaries on standby servers
370             Make sure the new binaries and support files are installed on
371             all standby servers.
372          2. Make sure the new standby data directories do not exist
373             Make sure the new standby data directories do not exist or are
374             empty. If initdb was run, delete the standby servers' new data
375             directories.
376          3. Install extension shared object files
377             Install the same extension shared object files on the new
378             standbys that you installed in the new primary cluster.
379          4. Stop standby servers
380             If the standby servers are still running, stop them now using
381             the above instructions.
382          5. Save configuration files
383             Save any configuration files from the old standbys'
384             configuration directories you need to keep, e.g.,
385             postgresql.conf (and any files included by it),
386             postgresql.auto.conf, pg_hba.conf, because these will be
387             overwritten or removed in the next step.
388          6. Run rsync
389             When using link mode, standby servers can be quickly upgraded
390             using rsync. To accomplish this, from a directory on the
391             primary server that is above the old and new database cluster
392             directories, run this on the primary for each standby server:
393 rsync --archive --delete --hard-links --size-only --no-inc-recursive old_cluster
394  new_cluster remote_dir
395
396             where old_cluster and new_cluster are relative to the current
397             directory on the primary, and remote_dir is above the old and
398             new cluster directories on the standby. The directory
399             structure under the specified directories on the primary and
400             standbys must match. Consult the rsync manual page for details
401             on specifying the remote directory, e.g.,
402 rsync --archive --delete --hard-links --size-only --no-inc-recursive /opt/Postgr
403 eSQL/12 \
404       /opt/PostgreSQL/18 standby.example.com:/opt/PostgreSQL
405
406             You can verify what the command will do using rsync's
407             --dry-run option. While rsync must be run on the primary for
408             at least one standby, it is possible to run rsync on an
409             upgraded standby to upgrade other standbys, as long as the
410             upgraded standby has not been started.
411             What this does is to record the links created by pg_upgrade's
412             link mode that connect files in the old and new clusters on
413             the primary server. It then finds matching files in the
414             standby's old cluster and creates links for them in the
415             standby's new cluster. Files that were not linked on the
416             primary are copied from the primary to the standby. (They are
417             usually small.) This provides rapid standby upgrades.
418             Unfortunately, rsync needlessly copies files associated with
419             temporary and unlogged tables because these files don't
420             normally exist on standby servers.
421             If you have tablespaces, you will need to run a similar rsync
422             command for each tablespace directory, e.g.:
423 rsync --archive --delete --hard-links --size-only --no-inc-recursive /vol1/pg_tb
424 lsp/PG_12_201909212 \
425       /vol1/pg_tblsp/PG_18_202307071 standby.example.com:/vol1/pg_tblsp
426
427             If you have relocated pg_wal outside the data directories,
428             rsync must be run on those directories too.
429          7. Configure streaming replication and log-shipping standby
430             servers
431             Configure the servers for log shipping. (You do not need to
432             run pg_backup_start() and pg_backup_stop() or take a file
433             system backup as the standbys are still synchronized with the
434             primary.) If the old primary is prior to version 17.0, then no
435             slots on the primary are copied to the new standby, so all the
436             slots on the old standby must be recreated manually. If the
437             old primary is version 17.0 or later, then only logical slots
438             on the primary are copied to the new standby, but other slots
439             on the old standby are not copied, so must be recreated
440             manually.
441    12. Restore pg_hba.conf
442        If you modified pg_hba.conf, restore its original settings. It
443        might also be necessary to adjust other configuration files in the
444        new cluster to match the old cluster, e.g., postgresql.conf (and
445        any files included by it), postgresql.auto.conf.
446    13. Start the new server
447        The new server can now be safely started, and then any rsync'ed
448        standby servers.
449    14. Post-upgrade processing
450        If any post-upgrade processing is required, pg_upgrade will issue
451        warnings as it completes. It will also generate script files that
452        must be run by the administrator. The script files will connect to
453        each database that needs post-upgrade processing. Each script
454        should be run using:
455 psql --username=postgres --file=script.sql postgres
456
457        The scripts can be run in any order and can be deleted once they
458        have been run.
459
460 Caution
461        In general it is unsafe to access tables referenced in rebuild
462        scripts until the rebuild scripts have run to completion; doing so
463        could yield incorrect results or poor performance. Tables not
464        referenced in rebuild scripts can be accessed immediately.
465    15. Statistics
466        Unless the --no-statistics option is specified, pg_upgrade will
467        transfer most optimizer statistics from the old cluster to the new
468        cluster. However, some statistics may not be transferred, such as
469        those created explicitly with CREATE STATISTICS or custom
470        statistics added by an extension.
471        Because not all statistics are transferred by pg_upgrade, you will
472        be instructed to run commands to regenerate that information at the
473        end of the upgrade. You might need to set connection parameters to
474        match your new cluster.
475        First, use vacuumdb --all --analyze-in-stages --missing-stats-only
476        to quickly generate minimal optimizer statistics for relations
477        without any. Then, use vacuumdb --all --analyze-only to ensure all
478        relations have updated cumulative statistics for triggering vacuum
479        and analyze. For both commands, the use of --jobs can speed it up.
480        If vacuum_cost_delay is set to a non-zero value, this can be
481        overridden to speed up statistics generation using PGOPTIONS, e.g.,
482        PGOPTIONS='-c vacuum_cost_delay=0' vacuumdb ....
483    16. Delete old cluster
484        Once you are satisfied with the upgrade, you can delete the old
485        cluster's data directories by running the script mentioned when
486        pg_upgrade completes. (Automatic deletion is not possible if you
487        have user-defined tablespaces inside the old data directory.) You
488        can also delete the old installation directories (e.g., bin,
489        share).
490    17. Reverting to old cluster
491        If, after running pg_upgrade, you wish to revert to the old
492        cluster, there are several options:
493           + If the --check option was used, the old cluster was
494             unmodified; it can be restarted.
495           + If neither --link nor --swap was used, the old cluster was
496             unmodified; it can be restarted.
497           + If the --link option was used, the data files might be shared
498             between the old and new cluster:
499                o If pg_upgrade aborted before linking started, the old
500                  cluster was unmodified; it can be restarted.
501                o If you did not start the new cluster, the old cluster was
502                  unmodified except that, when linking started, a .old
503                  suffix was appended to $PGDATA/global/pg_control. To
504                  reuse the old cluster, remove the .old suffix from
505                  $PGDATA/global/pg_control; you can then restart the old
506                  cluster.
507                o If you did start the new cluster, it has written to
508                  shared files and it is unsafe to use the old cluster. The
509                  old cluster will need to be restored from backup in this
510                  case.
511           + If the --swap option was used, the old cluster might be
512             destructively modified:
513                o If pg_upgrade aborts before reporting that the old
514                  cluster is no longer safe to start, the old cluster was
515                  unmodified; it can be restarted.
516                o If pg_upgrade has reported that the old cluster is no
517                  longer safe to start, the old cluster was destructively
518                  modified. The old cluster will need to be restored from
519                  backup in this case.
520
521 Environment
522
523    Some environment variables can be used to provide defaults for
524    command-line options:
525
526    PGBINOLD
527           The old PostgreSQL executable directory; option -b/--old-bindir.
528
529    PGBINNEW
530           The new PostgreSQL executable directory; option -B/--new-bindir.
531
532    PGDATAOLD
533           The old database cluster configuration directory; option
534           -d/--old-datadir.
535
536    PGDATANEW
537           The new database cluster configuration directory; option
538           -D/--new-datadir.
539
540    PGPORTOLD
541           The old cluster port number; option -p/--old-port.
542
543    PGPORTNEW
544           The new cluster port number; option -P/--new-port.
545
546    PGSOCKETDIR
547           Directory to use for postmaster sockets during upgrade; option
548           -s/--socketdir.
549
550    PGUSER
551           Cluster's install user name; option -U/--username.
552
553 Notes
554
555    pg_upgrade creates various working files, such as schema dumps, stored
556    within pg_upgrade_output.d in the directory of the new cluster. Each
557    run creates a new subdirectory named with a timestamp formatted as per
558    ISO 8601 (%Y%m%dT%H%M%S), where all its generated files are stored.
559    pg_upgrade_output.d and its contained files will be removed
560    automatically if pg_upgrade completes successfully; but in the event of
561    trouble, the files there may provide useful debugging information.
562
563    pg_upgrade launches short-lived postmasters in the old and new data
564    directories. Temporary Unix socket files for communication with these
565    postmasters are, by default, made in the current working directory. In
566    some situations the path name for the current directory might be too
567    long to be a valid socket name. In that case you can use the -s option
568    to put the socket files in some directory with a shorter path name. For
569    security, be sure that that directory is not readable or writable by
570    any other users. (This is not supported on Windows.)
571
572    All failure, rebuild, and reindex cases will be reported by pg_upgrade
573    if they affect your installation; post-upgrade scripts to rebuild
574    tables and indexes will be generated automatically. If you are trying
575    to automate the upgrade of many clusters, you should find that clusters
576    with identical database schemas require the same post-upgrade steps for
577    all cluster upgrades; this is because the post-upgrade steps are based
578    on the database schemas, and not user data.
579
580    For deployment testing, create a schema-only copy of the old cluster,
581    insert dummy data, and upgrade that.
582
583    pg_upgrade does not support upgrading of databases containing table
584    columns using these reg* OID-referencing system data types:
585    regcollation
586    regconfig
587    regdictionary
588    regnamespace
589    regoper
590    regoperator
591    regproc
592    regprocedure
593
594    (regclass, regrole, and regtype can be upgraded.)
595
596    If you want to use link mode and you do not want your old cluster to be
597    modified when the new cluster is started, consider using the clone
598    mode. If that is not available, make a copy of the old cluster and
599    upgrade that in link mode. To make a valid copy of the old cluster, use
600    rsync to create a dirty copy of the old cluster while the server is
601    running, then shut down the old server and run rsync --checksum again
602    to update the copy with any changes to make it consistent. (--checksum
603    is necessary because rsync only has file modification-time granularity
604    of one second.) You might want to exclude some files, e.g.,
605    postmaster.pid, as documented in Section 25.3.4. If your file system
606    supports file system snapshots or copy-on-write file copies, you can
607    use that to make a backup of the old cluster and tablespaces, though
608    the snapshot and copies must be created simultaneously or while the
609    database server is down.
610
611 See Also
612
613    initdb, pg_ctl, pg_dump, postgres