Saturday, May 15, 2010

Upgrading PostgreSQL 8.1 to 8.4 on CentOS 5.5

PostgreSQL 8.4 (postgresql84) is now included as a fully supported option in Red Hat Enterprise Linux 5.5/CentOS 5.5.

New features in PostgreSQL 8.4 include: parallel database restore, per-column permissions and new monitoring tools.

A data dump and restore using pg_dump is required for migration from the existing PostgreSQL 8.1 (supplied by the postgres package). Due to this requirement, postgres and postgresql84 contain package level conflicts and only a single version can be installed on a system.

To upgrade PostgreSQL, follow these steps:
  1. Create a backups directory:
    # mkdir /pgbak
    # chown postgres:postgres /pgbak/
    
  2. Dump only global database objects (roles and tablespaces), no databases:
    # su - postgres
    # pg_dumpall --globals-only > /pgbak/globals.sql 
    # pg_dump --create --oids --format=c --verbose --file=/pgbak/dbX dbX
    # exit
    
  3. Stop the database server:
    # /etc/init.d/postgresql stop
    
  4. Create full backup of database cluster (is not used in upgrading process):
    # mv /var/lib/pgsql/data /pgbak
    
  5. Upgrade PostgreSQL 8.1.6 to 8.4.2:
    # yum remove postgresql*
    # yum install postgresql84-server
    
  6. Initialize new database cluster:
    # /etc/init.d/postgresql initdb
    
  7. Restore configuration files modifications on /var/lib/pgsql/data/*.conf
  8. Start the PostgreSQL service:
    # chkconfig --level 35 postgresql on
    # /etc/init.d/postgresql start
    
  9. Restore global database objects (roles and tablespaces) and databases:
    # su - postgres
    psql -f /pgbak/globals.sql
    pg_restore --create -d postgres /pgbak/dbX
    # exit
    

8 comments:

  1. some remarks

    step 2: Dump only global database objects (roles and tablespaces), no databases:

    databases are dumped at this step :)

    pg_dumpall --globals-only -f /pgbak/globals.sql
    should be
    pg_dumpall --globals-only > /pgbak/globals.sql

    Thanks for the blog post!
    ReplyDelete
  2. Thank you. This is my mistake.
    ReplyDelete
  3. root@server7 [~]# /etc/init.d/postgresql initdb
    Initializing database: mkdir: cannot create directory `/var/lib/pgsql/data/pg_log': File exists
    [FAILED]
    ReplyDelete
  4. Be very, very careful following the above procedure as for some reason the yum remove postgresql* removed ALL the following packages on my CentOS 5.5 installation and I had to restore the entire server build from backup:

    Packages Erased:
    postgresql
    httpd-devel
    httpd-manual
    system-config-httpd
    webalizer
    php-devel
    postgresql-libs
    httpd
    mod_ssl
    php-pgsql
    subversion
    php
    php-pear
    apr-util-devel
    gnome-user-share
    dovecot
    apr-util
    mod_perl
    postgresql-server
    mod_python
    ReplyDelete
  5. The process which eventually worked successfully for me is given below:

    lynx http://cd.bromley.ac.uk/compat-postgresql-libs-4-1PGDG.rhel5.i686.rpm
    rpm -iv --replacefiles compat-postgresql-libs-4-1PGDG.rhel5.i686.rpm
    mkdir /pgbak
    chown postgres:postgres /pgbak
    su - postgres
    pg_dumpall > /pgbak/backup.sql
    exit
    service postgresql stop
    cp /var/lib/pgsql/data/pg_hba.conf /pgbak
    mv /var/lib/pgsql/data /pgbak
    yum remove postgresql*
    yum install postgresql84-server
    /etc/init.d/postgresql initdb
    cp /pgbak/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf
    chkconfig --level 35 postgresql on
    service postgresql start
    su - postgres
    psql -f /pgbak/backup.sql postgres
    vacuumdb -a -z
    exit
    ReplyDelete
  6. It seems that 'service postgresql start' command do not work... it said failed in my case... Help me out if you know the answer !!

    Thanks,
    Steve
    ReplyDelete
  7. Thanks, clive! Your walkthrough helped me to avoid removing those necessary packages!
    ReplyDelete