Sunday, October 2, 2011

Postgres 9.0 to 9.1 upgrade - Gentoo

If your running a virtual machine, with an automatic upgrade script you may find you need up upgrade your data to work with 9.1, and that the old version has been removed. To do the upgrade you need both version installed.

First step, install the old version of 9.0, if its not already there. You can check via:

eselect postgresql list

Check /var/lib/postgres, and see if the 9.0 data directory exists. Before doing anything else, make a copy this folder to a safe place, so you can revert if required.

If eselect didn't come up with any 9.0 version, install it using this.

emerge =dev-db/postgresql-server-9.0.5

Try and start 9.05, and make sure there are no errors. I found I had to move some configuration files around, and change some permissions, but the errors were very explicit and easy to solve.

Now, copy 9.0's configuration files.

cp -R /etc/postgresql-9.0/* /etc/postgresql-9.1/

Next step, create a blank 9.1 database in /var/lib/postgresql/9.1/data

su postgres

initdb91 /var/lib/postgresql/9.1/data

Make sure 9.1 can start.

/etc/init.d/postgresql-9.1 start

Stop it again, and your ready to run the conversion.

su postgres

pg_upgrade91 -v --old-datadir=/var/lib/postgresql/9.0/data/ --new-datadir=/var/lib/postgresql/9.1/data --old-bindir=/usr/lib/postgresql-9.0/bin/ --new-bindir=/usr/lib/postgresql-9.1/bin/

If this completes, start 9.1. You can run it using the init.d script, but I found I had an error which was not going to the logs. You can debug it by running pg_ctrl directly, which will print any errors to the screen.

su -l postgres -c 'env PGPORT="5432" /usr/lib/postgresql-9.1/bin/pg_ctl start -w -t 60 -s -D /var/lib/postgresql/9.1/data -o '\''-D /etc/postgresql-9.1/ --data-directory=/var/lib/postgresql/9.1/data'\'''

If everything is fine, your data migration should be complete, and all thats left to do is tidy up by removing 9.0's binary and the old data. i.e

emerge --unmerge dev-db/postgresql-server-9.0.5

8 comments:

Anonymous said...

pg_upgrade91 didn't work for me as shown because Gentoo puts the configuration files in /etc and pg_upgrade91 expects them in the data directory. I had to symlink /etc/postgresql-9.[01]/postgresql.conf and pg_hba.conf into each data directory. I also had to temporarily undo my tighter restrictions on postmaster login to the new cluster since the password wasn't set yet.

Ben Leov said...

I wasn't very clear on this step, thanks for the comment!

Neioo said...

Hello your guide works fine, but some typo errors must be corrected:

"emerge =dev-db/postgresql-server-9.0.3" sould be
"emerge =dev-db/postgresql-server-9.0.5" (9.0.3 is not into repository)

"cp -R /etc/postgresql-9.0/* /etc/postgresql-9.1/*" sould be "cp -R /etc/postgresql-9.0/* /etc/postgresql-9.1/" (remove last '*')

"su posgres" is "su postgres"

"/etc/init.d/postgres-9.1 start" sould be "/etc/init.d/postgresql-9.1 start"

Please can you correct typo errors?

Thank you very much :)

Benjamin Leov said...

Wow, thanks a lot for taking the time to comment on this Neioo. I have updated the post with your corrections.

My apologies for writing something with so many typos!

andersas said...

Postgresql is simply braindamaged. After upgrading a production server I had to go through this process with all 600 GB of our data. Luckily we had just that extra amount of free space.

I will not be installing postgresql in the future.

Benjamin Leov said...

Hi andersas,

Luckily, for me, I didn't have anywhere near that amount of data to migrate. What do you think you'll switch to?

velaia said...

This worked for me like a charm. 6.6GB of OSM Data converted in less than a minute, wow! Thanks!

One minor correction:

"emerge --unmerge dev-db/postgresql-server-9.0.3" should be "emerge --unmerge dev-db/postgresql-server-9.0.5" like Neioo commented for the emerge part.

Benjamin Leov said...

Post has been updated. Thanks for the comment!