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:

    tar xjfv postgresql-9.2.x.tar.bz2

    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/
    make -j y world
    sudo make install-world

    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/'

    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:


    Under ## EDIT FROM HERE add:


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

    touch $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

    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.


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

  1. Reading this article, I could set up postgresql from source with out a hiccup.
    Thank you.

  2. 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 *