Postgres is the gift of timesuck. It’s a great database, especially for spatial data. However, without insider knowledge, simple tasks eat time.
Latest example: pg_upgrade. It shrinks upgrade complexity–if it works! For Windows users, the documentation has useless and incomplete steps.
Here’s a better procedure. I used it to upgrade from 9.3 to 9.4. You might substitute for any older and newer version? I assume you’re using the default x64 install location of C:\Program Files\PostgreSQL. The steps:
- Install 9.4 with the Windows installer. Don’t let it use TCP port 5432 yet; that’s used by your current Postgres install. The installer saw this and recommended 5433. Add needed extensions with the Application Stack Builder. If you use PostGIS, don’t install the sample database. Do nothing else with the new install.
- With the Services control panel, shut down both the old and new databases.
- Create a new user on your PC named postgres. This is a Windows user, not a database user. Add it to your PC’s Administrators group. (I didn’t have this account. I don’t know why Postgres or pg_upgrade need it. A better design would permit me to specify database accounts for each install with pg_upgrade command line switches. The –username switch didn’t appear to do that, plus it would use the same username across both databases, which may not always be proper.)
- With Windows Explorer, give the Windows postgres account Full Control permission on C:\Program Files\PostgreSQL\ and all children.
- Edit the pg_hba.conf files for both servers. They are in C:\Program Files\PostgreSQL\[version]\data. If they are stock, comment out ALL lines, then add the below lines at the bottom. Note that this puts your database in an insecure state; you may want to take the host off the network or aggressively firewall it while this configuration is active. Be sure to save a copy of the current files so you can revert. The new lines:
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
- Open a command prompt window in administrator mode. Run this: RUNAS /USER:postgres “CMD.EXE”
- In the new command prompt window, run this: pg_upgrade.exe –old-datadir “C:/Program Files/PostgreSQL/9.3/data” –new-datadir “C:/Program Files/PostgreSQL/9.4/data” –old-bindir “C:/Program Files/PostgreSQL/9.3/bin” –new-bindir “C:/Program Files/PostgreSQL/9.4/bin” This will take a while to run if you have a lot of data. Wait until this is done before continuing.
- Edit the postgresql.conf file for the new Postgres install and change the listening port to 5432 (from 5433).
- Through the Services control panel, start your new Postgres install back up.
- Vacuum and reanalyze all databases.
- If you created a postgres Windows account above, remove it.
The new Postgres install’s postgres database account (not the Windows account) will now have the same password as in the old Postgres install.
Once you’ve verified that everything works properly, it may be safe to uninstall the old Postgres copy.