F.32. pg_upgrade

pg_upgrade (formerly called pg_migrator) allows data stored in PostgreSQL data files to be migrated to a later PostgreSQL major version without the data dump/reload typically required for major version upgrades, e.g. from 8.4.7 to the current major release of PostgreSQL. It is not required for minor version upgrades, e.g. from 9.0.1 to 9.0.4.

pg_upgrade works because, though new features are regularly added to PostgreSQL major releases, the internal data storage format rarely changes. pg_upgrade does its best to make sure the old and new clusters are binary-compatible, e.g. by checking for compatible compile-time settings, including 32/64-bit binaries. It is important that any external modules are also binary compatible, though this cannot be checked by pg_upgrade.

F.32.1. Supported Versions

pg_upgrade supports upgrades from 8.3.X and later to the current major release of PostgreSQL, including snapshot and alpha releases.

F.32.2. pg_upgrade Options

pg_upgrade accepts the following command-line arguments:

-b old_bindir
--old-bindir OLDBINDIR

specify the old cluster executable directory

-B new_bindir
--new-bindir NEWBINDIR

specify the new cluster executable directory


check clusters only, don't change any data

-d old_datadir
--old-datadir OLDDATADIR

specify the old cluster data directory

-D new_datadir
--new-datadir NEWDATADIR

specify the new cluster data directory


enable debugging

-G debug_filename

output debugging activity to file


link instead of copying files to new cluster

-l log_filename

log session activity to file

-p old_portnum
--old-port portnum

specify the old cluster port number

-P new_portnum
--new-port portnum

specify the new cluster port number

-u username
--user username

clusters superuser


enable verbose output


display version information, then exit


show help, then exit

F.32.3. Upgrade Steps

  1. Optionally move the old cluster

    If you are using a version-specific installation directory, e.g. /opt/PostgreSQL/8.4, you do not need to move the old cluster. The one-click installers all use version-specific installation directories.

    If your installation directory is not version-specific, e.g. /usr/local/pgsql, it is necessary to move the current PostgreSQL install directory so it does not interfere with the new PostgreSQL installation. Once the current PostgreSQL server is shut down, it is safe to rename the PostgreSQL installation directory; assuming the old directory is /usr/local/pgsql, you can do:

    mv /usr/local/pgsql /usr/local/pgsql.old

    to rename the directory.

  2. For source installs, build the new version

    Build the new PostgreSQL source with configure flags that are compatible with the old cluster. pg_upgrade will check pg_controldata to make sure all settings are compatible before starting the upgrade.

  3. Install the new PostgreSQL binaries

    Install the new server's binaries and support files. You can use the same port numbers for both clusters, typically 5432, because the old and new clusters will not be running at the same time.

    For source installs, if you wish to install the new server in a custom location, use the prefix variable:

    gmake prefix=/usr/local/pgsql.new install

  4. Install pg_upgrade and pg_upgrade_support

    Install pg_upgrade and pg_upgrade_support in the new PostgreSQL cluster

  5. Initialize the new PostgreSQL cluster

    Initialize the new cluster using initdb. Again, use compatible initdb flags that match the old cluster. Many prebuilt installers do this step automatically. There is no need to start the new cluster.

  6. Install custom shared object files

    Install any custom shared object files (or DLLs) used by the old cluster into the new cluster, e.g. pgcrypto.so, whether they are from contrib or some other source. Do not install the schema definitions, e.g. pgcrypto.sql, because these will be migrated from the old cluster.

  7. Adjust authentication

    pg_upgrade will connect to the old and new servers several times, so you might want to set authentication to trust in pg_hba.conf, or if using md5 authentication, use a ~/.pgpass file (see Section 31.14) to avoid being prompted repeatedly for a password.

  8. Stop both servers

    Make sure both database servers are stopped using, on Unix, e.g.:

    pg_ctl -D /opt/PostgreSQL/8.4 stop
    pg_ctl -D /opt/PostgreSQL/9.0 stop

    or on Windows, using the proper service names:

    NET STOP postgresql-8.4
    NET STOP postgresql-9.0


    NET STOP pgsql-8.3  (PostgreSQL 8.3 and older used a different service name)

  9. Run pg_upgrade

    Always run the pg_upgrade binary of the new server, not the old one. pg_upgrade requires the specification of the old and new cluster's data and executable (bin) directories. You can also specify separate user and port values, and whether you want the data linked instead of copied (the default). If you use linking, the migration will be much faster (no data copying), but you will no longer be able to access your old cluster once you start the new cluster after the upgrade. See pg_upgrade --help for a full list of options.

    For Windows users, you must be logged into an administrative account, and then start a shell as the postgres user and set the proper path:

    RUNAS /USER:postgres "CMD.EXE"
    SET PATH=%PATH%;C:\Program Files\PostgreSQL\9.0\bin;

    and then run pg_upgrade with quoted directories, e.g.:

            --old-datadir "C:/Program Files/PostgreSQL/8.4/data"
            --new-datadir "C:/Program Files/PostgreSQL/9.0/data"
            --old-bindir "C:/Program Files/PostgreSQL/8.4/bin"
            --new-bindir "C:/Program Files/PostgreSQL/9.0/bin"

    Once started, pg_upgrade will verify the two clusters are compatible and then do the migration. You can use pg_upgrade --check to perform only the checks, even if the old server is still running. pg_upgrade --check will also outline any manual adjustments you will need to make after the migration. pg_upgrade requires write permission in the current directory.

    Obviously, no one should be accessing the clusters during the migration.

    If an error occurs while restoring the database schema, pg_upgrade will exit and you will have to revert to the old cluster as outlined in step 14 below. To try pg_upgrade again, you will need to modify the old cluster so the pg_upgrade schema restore succeeds. If the problem is a contrib module, you might need to uninstall the contrib module from the old cluster and install it in the new cluster after the migration, assuming the module is not being used to store user data.

  10. Restore pg_hba.conf

    If you modified pg_hba.conf to use trust, restore its original authentication settings.

  11. Post-migration processing

    If any post-migration processing is required, pg_upgrade will issue warnings as it completes. It will also generate script files that must be run by the administrator. The script files will connect to each database that needs post-migration processing. Each script should be run using:

    psql --username postgres --file script.sql postgres

    The scripts can be run in any order and can be deleted once they have been run.


    In general it is unsafe to access tables referenced in rebuild scripts until the rebuild scripts have run to completion; doing so could yield incorrect results or poor performance. Tables not referenced in rebuild scripts can be accessed immediately.

  12. Statistics

    Because optimizer statistics are not transferred by pg_upgrade, you will be instructed to run a command to regenerate that information at the end of the migration.

  13. Delete old cluster

    Once you are satisfied with the upgrade, you can delete the old cluster's data directories by running the script mentioned when pg_upgrade completes. You can also delete the old installation directories (e.g. bin, share).

  14. Reverting to old cluster

    If, after running pg_upgrade, you wish to revert to the old cluster, there are several options:

    • If you ran pg_upgrade with --check, no modifications were made to the old cluster and you can re-use it anytime.

    • If you ran pg_upgrade with --link, the data files are shared between the old and new cluster. If you started the new cluster, the new server has written to those shared files and it is unsafe to use the old cluster.

    • If you ran pg_upgrade without --link or did not start the new server, the old cluster was not modified except that an .old suffix was appended to $PGDATA/global/pg_control and perhaps tablespace directories. To reuse the old cluster, remove the .old suffix from $PGDATA/global/pg_control. and, if migrating to 8.4 or earlier, remove the tablespace directories created by the migration and remove the .old suffix from the tablespace directory names; then you can restart the old cluster.

F.32.4. Limitations in Migrating from PostgreSQL 8.3

Upgrading from PostgreSQL 8.3 has additional restrictions not present when upgrading from later PostgreSQL releases. For example, pg_upgrade will not work for a migration from 8.3 if a user column is defined as:

You must drop any such columns and migrate them manually.

pg_upgrade will require a table rebuild if:

pg_upgrade will require a reindex if:

Also, the default datetime storage format changed to integer after PostgreSQL 8.3. pg_upgrade will check that the datetime storage format used by the old and new clusters match. Make sure your new cluster is built with the configure flag --disable-integer-datetimes.

For Windows users, note that due to different integer datetimes settings used by the one-click installer and the MSI installer, it is only possible to upgrade from version 8.3 of the one-click distribution to version 8.4 or later of the one-click distribution. It is not possible to upgrade from the MSI installer to the one-click installer.

F.32.5. Notes

pg_upgrade does not support migration of databases containing these reg* OID-referencing system data types: regproc, regprocedure, regoper, regoperator, regclass, regconfig, and regdictionary. (regtype can be migrated.)

All failure, rebuild, and reindex cases will be reported by pg_upgrade if they affect your installation; post-migration scripts to rebuild tables and indexes will be generated automatically.

For deployment testing, create a schema-only copy of the old cluster, insert dummy data, and migrate that.

If you want to use link mode and you don't want your old cluster to be modified when the new cluster is started, make a copy of the old cluster and migrate that with link mode. To make a valid copy of the old cluster, use rsync to create a dirty copy of the old cluster while the server is running, then shut down the old server and run rsync again to update the copy with any changes to make it consistent.