<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >pg_upgrade</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.0.22 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Additional Supplied Modules" HREF="contrib.html"><LINK REL="PREVIOUS" TITLE="pg_trgm" HREF="pgtrgm.html"><LINK REL="NEXT" TITLE="seg" HREF="seg.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2015-06-13T20:14:15"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.0.22 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="pg_trgm" HREF="pgtrgm.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="contrib.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Appendix F. Additional Supplied Modules</TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="seg" HREF="seg.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="PGUPGRADE" >F.32. pg_upgrade</A ></H1 ><P > <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > (formerly called <SPAN CLASS="APPLICATION" >pg_migrator</SPAN >) allows data stored in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > data files to be migrated to a later <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 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 <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >. It is not required for minor version upgrades, e.g. from 9.0.1 to 9.0.4. </P ><P > <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > works because, though new features are regularly added to PostgreSQL major releases, the internal data storage format rarely changes. <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > 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 <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN >. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN130663" >F.32.1. Supported Versions</A ></H2 ><P > pg_upgrade supports upgrades from 8.3.X and later to the current major release of <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >, including snapshot and alpha releases. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN130667" >F.32.2. <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > Options</A ></H2 ><P > <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > accepts the following command-line arguments: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="OPTION" >-b</TT > <TT CLASS="REPLACEABLE" ><I >old_bindir</I ></TT ><BR><TT CLASS="OPTION" >--old-bindir</TT > <TT CLASS="REPLACEABLE" ><I >OLDBINDIR</I ></TT ></DT ><DD ><P >specify the old cluster executable directory</P ></DD ><DT ><TT CLASS="OPTION" >-B</TT > <TT CLASS="REPLACEABLE" ><I >new_bindir</I ></TT ><BR><TT CLASS="OPTION" >--new-bindir</TT > <TT CLASS="REPLACEABLE" ><I >NEWBINDIR</I ></TT ></DT ><DD ><P >specify the new cluster executable directory</P ></DD ><DT ><TT CLASS="OPTION" >-c</TT ><BR><TT CLASS="OPTION" >--check</TT ></DT ><DD ><P >check clusters only, don't change any data</P ></DD ><DT ><TT CLASS="OPTION" >-d</TT > <TT CLASS="REPLACEABLE" ><I >old_datadir</I ></TT ><BR><TT CLASS="OPTION" >--old-datadir</TT > <TT CLASS="REPLACEABLE" ><I >OLDDATADIR</I ></TT ></DT ><DD ><P >specify the old cluster data directory</P ></DD ><DT ><TT CLASS="OPTION" >-D</TT > <TT CLASS="REPLACEABLE" ><I >new_datadir</I ></TT ><BR><TT CLASS="OPTION" >--new-datadir</TT > <TT CLASS="REPLACEABLE" ><I >NEWDATADIR</I ></TT ></DT ><DD ><P >specify the new cluster data directory</P ></DD ><DT ><TT CLASS="OPTION" >-g</TT ><BR><TT CLASS="OPTION" >--debug</TT ></DT ><DD ><P >enable debugging</P ></DD ><DT ><TT CLASS="OPTION" >-G</TT > <TT CLASS="REPLACEABLE" ><I >debug_filename</I ></TT ><BR><TT CLASS="OPTION" >--debugfile</TT > <TT CLASS="REPLACEABLE" ><I >DEBUGFILENAME</I ></TT ></DT ><DD ><P >output debugging activity to file</P ></DD ><DT ><TT CLASS="OPTION" >-k</TT ><BR><TT CLASS="OPTION" >--link</TT ></DT ><DD ><P >use hard links instead of copying files to the new cluster</P ></DD ><DT ><TT CLASS="OPTION" >-l</TT > <TT CLASS="REPLACEABLE" ><I >log_filename</I ></TT ><BR><TT CLASS="OPTION" >--logfile</TT > <TT CLASS="REPLACEABLE" ><I >LOGFILENAME</I ></TT ></DT ><DD ><P >log session activity to file</P ></DD ><DT ><TT CLASS="OPTION" >-p</TT > <TT CLASS="REPLACEABLE" ><I >old_portnum</I ></TT ><BR><TT CLASS="OPTION" >--old-port</TT > <TT CLASS="REPLACEABLE" ><I >portnum</I ></TT ></DT ><DD ><P >specify the old cluster port number</P ></DD ><DT ><TT CLASS="OPTION" >-P</TT > <TT CLASS="REPLACEABLE" ><I >new_portnum</I ></TT ><BR><TT CLASS="OPTION" >--new-port</TT > <TT CLASS="REPLACEABLE" ><I >portnum</I ></TT ></DT ><DD ><P >specify the new cluster port number</P ></DD ><DT ><TT CLASS="OPTION" >-u</TT > <TT CLASS="REPLACEABLE" ><I >username</I ></TT ><BR><TT CLASS="OPTION" >--user</TT > <TT CLASS="REPLACEABLE" ><I >username</I ></TT ></DT ><DD ><P >clusters superuser</P ></DD ><DT ><TT CLASS="OPTION" >-v</TT ><BR><TT CLASS="OPTION" >--verbose</TT ></DT ><DD ><P >enable verbose output</P ></DD ><DT ><TT CLASS="OPTION" >-V</TT ><BR><TT CLASS="OPTION" >--version</TT ></DT ><DD ><P >display version information, then exit</P ></DD ><DT ><TT CLASS="OPTION" >-?</TT ><BR><TT CLASS="OPTION" >-h</TT ><BR><TT CLASS="OPTION" >--help</TT ></DT ><DD ><P >show help, then exit</P ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN130798" >F.32.3. Upgrade Steps</A ></H2 ><DIV CLASS="PROCEDURE" ><OL TYPE="1" ><LI CLASS="STEP" ><P ><B >Optionally move the old cluster</B ></P ><P > If you are using a version-specific installation directory, e.g. <TT CLASS="FILENAME" >/opt/PostgreSQL/8.4</TT >, you do not need to move the old cluster. The one-click installers all use version-specific installation directories. </P ><P > If your installation directory is not version-specific, e.g. <TT CLASS="FILENAME" >/usr/local/pgsql</TT >, it is necessary to move the current PostgreSQL install directory so it does not interfere with the new <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > installation. Once the current <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server is shut down, it is safe to rename the PostgreSQL installation directory; assuming the old directory is <TT CLASS="FILENAME" >/usr/local/pgsql</TT >, you can do: </P><PRE CLASS="PROGRAMLISTING" >mv /usr/local/pgsql /usr/local/pgsql.old</PRE ><P> to rename the directory. </P ></LI ><LI CLASS="STEP" ><P ><B >For source installs, build the new version</B ></P ><P > Build the new PostgreSQL source with <TT CLASS="COMMAND" >configure</TT > flags that are compatible with the old cluster. <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > will check <TT CLASS="COMMAND" >pg_controldata</TT > to make sure all settings are compatible before starting the upgrade. </P ></LI ><LI CLASS="STEP" ><P ><B >Install the new PostgreSQL binaries</B ></P ><P > 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. </P ><P > For source installs, if you wish to install the new server in a custom location, use the <TT CLASS="LITERAL" >prefix</TT > variable: </P><PRE CLASS="PROGRAMLISTING" >gmake prefix=/usr/local/pgsql.new install</PRE ><P> </P ></LI ><LI CLASS="STEP" ><P ><B >Install pg_upgrade and pg_upgrade_support</B ></P ><P > Install <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > and <SPAN CLASS="APPLICATION" >pg_upgrade_support</SPAN > in the new PostgreSQL cluster </P ></LI ><LI CLASS="STEP" ><P ><B >Initialize the new PostgreSQL cluster</B ></P ><P > Initialize the new cluster using <TT CLASS="COMMAND" >initdb</TT >. Again, use compatible <TT CLASS="COMMAND" >initdb</TT > flags that match the old cluster. Many prebuilt installers do this step automatically. There is no need to start the new cluster. </P ></LI ><LI CLASS="STEP" ><P ><B >Install custom shared object files</B ></P ><P > Install any custom shared object files (or DLLs) used by the old cluster into the new cluster, e.g. <TT CLASS="FILENAME" >pgcrypto.so</TT >, whether they are from <TT CLASS="FILENAME" >contrib</TT > or some other source. Do not install the schema definitions, e.g. <TT CLASS="FILENAME" >pgcrypto.sql</TT >, because these will be migrated from the old cluster. </P ></LI ><LI CLASS="STEP" ><P ><B >Adjust authentication</B ></P ><P > <TT CLASS="COMMAND" >pg_upgrade</TT > will connect to the old and new servers several times, so you might want to set <TT CLASS="LITERAL" >local</TT > Unix-domain socket authentication to <TT CLASS="LITERAL" >ident</TT > in <TT CLASS="FILENAME" >pg_hba.conf</TT > or use a <TT CLASS="FILENAME" >~/.pgpass</TT > file (see <A HREF="libpq-pgpass.html" >Section 31.14</A >). </P ></LI ><LI CLASS="STEP" ><P ><B >Stop both servers</B ></P ><P > Make sure both database servers are stopped using, on Unix, e.g.: </P><PRE CLASS="PROGRAMLISTING" >pg_ctl -D /opt/PostgreSQL/8.4 stop pg_ctl -D /opt/PostgreSQL/9.0 stop</PRE ><P> or on Windows, using the proper service names: </P><PRE CLASS="PROGRAMLISTING" >NET STOP postgresql-8.4 NET STOP postgresql-9.0</PRE ><P> or </P><PRE CLASS="PROGRAMLISTING" >NET STOP pgsql-8.3 (<SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 8.3 and older used a different service name)</PRE ><P> </P ></LI ><LI CLASS="STEP" ><P ><B >Run <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN ></B ></P ><P > Always run the <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > binary of the new server, not the old one. <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > requires the specification of the old and new cluster's data and executable (<TT CLASS="FILENAME" >bin</TT >) directories. You can also specify user and port values, and whether you want the data linked instead of copied (the default). </P ><P > If you use link mode, the upgrade will be much faster (no file copying), but you will not be able to access your old cluster once you start the new cluster after the upgrade. Link mode also requires that the old and new cluster data directories be in the same file system. See <TT CLASS="LITERAL" >pg_upgrade --help</TT > for a full list of options. </P ><P > For Windows users, you must be logged into an administrative account, and then start a shell as the <TT CLASS="LITERAL" >postgres</TT > user and set the proper path: </P><PRE CLASS="PROGRAMLISTING" >RUNAS /USER:postgres "CMD.EXE" SET PATH=%PATH%;C:\Program Files\PostgreSQL\9.0\bin;</PRE ><P> and then run <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > with quoted directories, e.g.: </P><PRE CLASS="PROGRAMLISTING" >pg_upgrade.exe --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"</PRE ><P> Once started, <TT CLASS="COMMAND" >pg_upgrade</TT > will verify the two clusters are compatible and then do the migration. You can use <TT CLASS="COMMAND" >pg_upgrade --check</TT > to perform only the checks, even if the old server is still running. <TT CLASS="COMMAND" >pg_upgrade --check</TT > will also outline any manual adjustments you will need to make after the migration. <TT CLASS="COMMAND" >pg_upgrade</TT > requires write permission in the current directory. </P ><P > Obviously, no one should be accessing the clusters during the migration. Consider using a non-default port number, e.g. 50432, for old and new clusters to avoid unintended client connections during the upgrade. </P ><P > If an error occurs while restoring the database schema, <TT CLASS="COMMAND" >pg_upgrade</TT > will exit and you will have to revert to the old cluster as outlined in <A HREF="pgupgrade.html#PGUPGRADE-STEP-REVERT" >step 14</A > below. To try <TT CLASS="COMMAND" >pg_upgrade</TT > 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. </P ></LI ><LI CLASS="STEP" ><P ><B >Restore <TT CLASS="FILENAME" >pg_hba.conf</TT ></B ></P ><P > If you modified <TT CLASS="FILENAME" >pg_hba.conf</TT >, restore its original settings. </P ></LI ><LI CLASS="STEP" ><P ><B >Post-migration processing</B ></P ><P > 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: </P><PRE CLASS="PROGRAMLISTING" >psql --username postgres --file script.sql postgres</PRE ><P> The scripts can be run in any order and can be deleted once they have been run. </P ><DIV CLASS="CAUTION" ><P ></P ><TABLE CLASS="CAUTION" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Caution</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > 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. </P ></TD ></TR ></TABLE ></DIV ></LI ><LI CLASS="STEP" ><P ><B >Statistics</B ></P ><P > Because optimizer statistics are not transferred by <TT CLASS="COMMAND" >pg_upgrade</TT >, you will be instructed to run a command to regenerate that information at the end of the migration. </P ></LI ><LI CLASS="STEP" ><P ><B >Delete old cluster</B ></P ><P > Once you are satisfied with the upgrade, you can delete the old cluster's data directories by running the script mentioned when <TT CLASS="COMMAND" >pg_upgrade</TT > completes. You can also delete the old installation directories (e.g. <TT CLASS="FILENAME" >bin</TT >, <TT CLASS="FILENAME" >share</TT >). </P ></LI ><LI CLASS="STEP" ><A NAME="PGUPGRADE-STEP-REVERT" ></A ><P ><B >Reverting to old cluster</B ></P ><P > If, after running <TT CLASS="COMMAND" >pg_upgrade</TT >, you wish to revert to the old cluster, there are several options: <P ></P ></P><UL ><LI ><P > If you ran <TT CLASS="COMMAND" >pg_upgrade</TT > with <TT CLASS="OPTION" >--check</TT >, no modifications were made to the old cluster and you can re-use it anytime. </P ></LI ><LI ><P > If you ran <TT CLASS="COMMAND" >pg_upgrade</TT > with <TT CLASS="OPTION" >--link</TT >, 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. </P ></LI ><LI ><P > If you ran <TT CLASS="COMMAND" >pg_upgrade</TT > <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >without</I ></SPAN > <TT CLASS="OPTION" >--link</TT > or did not start the new server, the old cluster was not modified except that an <TT CLASS="LITERAL" >.old</TT > suffix was appended to <TT CLASS="FILENAME" >$PGDATA/global/pg_control</TT > and perhaps tablespace directories. To reuse the old cluster, remove the <TT CLASS="FILENAME" >.old</TT > suffix from <TT CLASS="FILENAME" >$PGDATA/global/pg_control</TT >. and, if migrating to 8.4 or earlier, remove the tablespace directories created by the migration and remove the <TT CLASS="FILENAME" >.old</TT > suffix from the tablespace directory names; then you can restart the old cluster. </P ></LI ></UL ><P> </P ></LI ></OL ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN130922" >F.32.4. Limitations in Migrating <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >from</I ></SPAN > PostgreSQL 8.3</A ></H2 ><P > 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: <P ></P ></P><UL ><LI ><P > a <TT CLASS="TYPE" >tsquery</TT > data type </P ></LI ><LI ><P > data type <TT CLASS="TYPE" >name</TT > and is not the first column </P ></LI ></UL ><P> </P ><P > pg_upgrade will not work if the <TT CLASS="FILENAME" >ltree</TT > contrib module is installed in a database. </P ><P > You must drop any such columns and migrate them manually. </P ><P > pg_upgrade will require a table rebuild if: <P ></P ></P><UL ><LI ><P > a user column is of data type <TT CLASS="TYPE" >tsvector</TT > </P ></LI ></UL ><P> </P ><P > pg_upgrade will require a reindex if: <P ></P ></P><UL ><LI ><P > an index is of type hash or GIN </P ></LI ><LI ><P > an index uses <CODE CLASS="FUNCTION" >bpchar_pattern_ops</CODE > </P ></LI ></UL ><P> </P ><P > Also, the default datetime storage format changed to integer after <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 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 <TT CLASS="OPTION" >--disable-integer-datetimes</TT >. </P ><P > 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. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN130952" >F.32.5. Notes</A ></H2 ><P > <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > does not support migration of databases containing these <TT CLASS="TYPE" >reg*</TT > OID-referencing system data types: <TT CLASS="TYPE" >regproc</TT >, <TT CLASS="TYPE" >regprocedure</TT >, <TT CLASS="TYPE" >regoper</TT >, <TT CLASS="TYPE" >regoperator</TT >, <TT CLASS="TYPE" >regconfig</TT >, and <TT CLASS="TYPE" >regdictionary</TT >. (<TT CLASS="TYPE" >regtype</TT > can be migrated.) </P ><P > All failure, rebuild, and reindex cases will be reported by <SPAN CLASS="APPLICATION" >pg_upgrade</SPAN > if they affect your installation; post-migration scripts to rebuild tables and indexes will be generated automatically. </P ><P > For deployment testing, create a schema-only copy of the old cluster, insert dummy data, and migrate that. </P ><P > 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 <TT CLASS="COMMAND" >rsync</TT > to create a dirty copy of the old cluster while the server is running, then shut down the old server and run <TT CLASS="COMMAND" >rsync</TT > again to update the copy with any changes to make it consistent. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pgtrgm.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="seg.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_trgm</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="contrib.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >seg</TD ></TR ></TABLE ></DIV ></BODY ></HTML >