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