Browsed by
Month: June 2013

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

Compile and Install Redis 2.6.x from Source on Ubuntu 13.04 Server

Compile and Install Redis 2.6.x from Source on Ubuntu 13.04 Server

Working on a project for which I am using MySQL. As much as I love my relational databases they may not always be the best tool for the job. As such, for the first time, I am evaluating other solutions. Redis 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 make gcc

  • Download the latest source and extract:


    wget http://redis.googlecode.com/files/redis-2.6.x.tar.gz
    tar xzfv redis-2.6.x.tar.gz

    Note
    I am using v2.6.14 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 redis-2.6.x/
    make -j y
    sudo make install

    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 and open Redis configuration:


    sudo mkdir /etc/redis/
    sudo cp redis.conf /etc/redis/
    sudo nano /etc/redis/redis.conf

    Change the following options:


    daemonize yes
    dir /var/lib/redis/
    syslog-enabled yes
    syslog-ident redis
    syslog-facility local0

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

  • Remove working directory:


    cd ../
    rm -rf redis-2.6.x/
    rm redis-2.6.x.tar.gz

  • Add user:


    sudo useradd redis

  • Create data directories:


    sudo mkdir /var/lib/redis/
    sudo chown redis:redis /var/lib/redis/
    sudo mkdir /var/log/redis/
    sudo chown redis:redis /var/log/redis/

  • Setup start up script:


    sudo nano /etc/init.d/redis-server

    Within this file add the following and save/close:


    #! /bin/sh

    ### BEGIN INIT INFO
    # Provides: redis-server
    # Required-Start: $syslog $remote_fs
    # Required-Stop: $syslog $remote_fs
    # Should-Start: $local_fs
    # Should-Stop: $local_fs
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: redis-server - Persistent key-value db
    # Description: redis-server - Persistent key-value db
    ### END INIT INFO

    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DAEMON=/usr/local/bin/redis-server
    DAEMON_ARGS=/etc/redis/redis.conf
    NAME=redis-server
    DESC=redis-server
    PIDFILE=/var/run/redis.pid

    test -x $DAEMON || exit 0

    set -e

    case "$1" in
    start)
    echo -n "Starting $DESC: "

    touch $PIDFILE
    chown redis:redis $PIDFILE

    if start-stop-daemon --start --quiet --umask 007 --pidfile $PIDFILE --chuid redis:redis --exec $DAEMON -- $DAEMON_ARGS
    then
    echo "$NAME."
    else
    echo "failed"
    fi

    ;;

    stop)
    echo -n "Stopping $DESC: "

    if start-stop-daemon --stop --retry 10 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    then
    echo "$NAME."
    else
    echo "failed"
    fi

    rm -f $PIDFILE

    ;;

    restart|force-reload)
    ${0} stop
    ${0} start

    ;;

    status)
    echo -n "$DESC is "

    if start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
    then
    echo "running"
    else
    echo "not running"

    exit 1
    fi

    ;;

    *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2

    exit 1

    ;;
    esac

    exit 0

    Make script executable:


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

  • Start the server:


    sudo service redis-server start


Notes

  • I have not experimented with this on a 32-bit machine but I doubt it would be very useful for anything other than seeing what is what. If you are going to use this in anything remotely resembling a production environment I would use a 64-bit machine without question.
  • I disabled snapshotting for my tests as I will not need it. If you end up using it– such as in the default configuration– you might want to add vm.overcommit_memory = 1 to /etc/sysctl.conf.