Compile and Install PostgreSQL 9.2.x from Source on Ubuntu 13.04 Server

Compile and Install PostgreSQL 9.2.x from Source on Ubuntu 13.04 Server

Working on a project for which I am using MySQL. As much as I love the thing it has been scaring me a little since Oracle bought them. They have pledged to keep the project open-source friendly a while back but I still worry. PostgreSQL seems like it might be a good fit for me so here are the steps I took to get it up and running from source on Ubuntu 13.04 Server.

  • Make sure our required tools are installed:


    sudo apt-get update
    sudo apt-get install zlib1g-dev libreadline6-dev gcc make

  • Download the latest source and extract:


    wget ftp://ftp.postgresql.org/pub/source/v9.2.x/postgresql-9.2.x.tar.bz2
    tar xjfv postgresql-9.2.x.tar.bz2

    Note
    I am using v9.2.4 for this example as it was the latest at the time of this writing. Simply replace x with your version number.

  • Compile and install:


    cd postgresql-9.2.x/
    ./configure
    make -j y world
    sudo make install-world

    Note
    Replace y with the number of processor cores you would like to use. This step generally does not take long so if you wanted to omit -j and its argument all together that would also work.

  • Copy start up script:


    sudo cp contrib/start-scripts/linux /etc/init.d/postgresql

  • Remove working directory:


    cd ../
    rm -rf postgresql-9.2.x/
    rm postgresql-9.2.x.tar.bz2

  • Add user:


    sudo useradd postgres

  • Create data directories:


    sudo mkdir /var/lib/postgresql/
    sudo chown postgres:postgres /var/lib/postgresql/

    Create data:


    sudo su - postgres -c "/usr/local/pgsql/bin/initdb -D /var/lib/postgresql/"

  • Create configuration directory:


    sudo mkdir /etc/postgresql/
    sudo chown postgres:postgres /etc/postgresql/

    Move configuration:


    sudo mv /var/lib/postgresql/postgresql.conf /var/lib/postgresql/pg_hba.conf /var/lib/postgresql/pg_ident.conf /etc/postgresql/

    Open postgresql.conf:


    sudo nano /etc/postgresql/postgresql.conf

    Change postgresql.conf:


    data_directory = '/var/lib/postgresql/'
    external_pid_file = '/var/run/postgresql.pid'

    Note
    These are the bare minimum changes. You will probably want to scan the file and change it to better suit your needs.

  • Open start up script:


    sudo nano /etc/init.d/postgresql

    Change start up script:


    PGDATA="/etc/postgresql/"
    PGLOG="/var/log/postgresql.log"

    Under ## EDIT FROM HERE add:


    PGGROUP=postgres
    PGPID="/var/run/postgresql.pid"

    Above su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1, under start) add:


    touch $PGPID
    chown $PGUSER:$PGGROUP $PGPID

    Under su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast", under stop) add:


    rm $PGPID

  • Make start up script executable:


    sudo chmod +x /etc/init.d/postgresql
    sudo update-rc.d postgresql defaults

  • Start service:


    sudo service postgresql start

  • [Optional] Make commands available to all users:


    sudo ln -s /usr/local/pgsql/bin/* /usr/local/bin/

    [Optional] Make C includes and libraries available to all users:


    sudo ln -s /usr/local/pgsql/include/* /usr/local/include/
    sudo ln -s /usr/local/pgsql/lib/* /usr/local/lib/
    sudo ldconfig

    Note
    These just make life slightly easier. If you prefer you can always just call everything by their absolute paths or put them in your $PATH.


Sources
http://www.postgresql.org/docs/current/static/installation.html
http://www.postgresql.org/docs/9.2/static/server-start.html

3 thoughts on “Compile and Install PostgreSQL 9.2.x from Source on Ubuntu 13.04 Server

  1. Hi Nick, please help me out here i really enjoyed this guide, as i didn’t know what to do after installing from source following instructions fro postgresql official site. That being the case please direct me on what to do as in my case its Ubuntu 14.04 LTS and postgresql 9.4.5. At this “Under su – $PGUSER -c “$PGCTL stop -D ‘$PGDATA’ -s -m fast”, under stop) add:” step in my “init.d” file i don’t see the “stop)” to add “rm $PGPID” so I dropped it just under the “su” line. Please direct me on where to rightly drop it.
    And thanks again for the great effort.

Leave a Reply

Your email address will not be published. Required fields are marked *