In part 1, we installed PostgreSQL on the VPS. In this part, it’s time to get the database server up and configured.

First, though, now that we have something to lose on the server, backups are now enabled at Slicehost. $5/month is cheap insurance. This also means we can take a snapshot of the slice, which we’ll do at the end of each major checkpoint, so we can roll back to it if something gets screwed up. (As they say in videogaming, “Hope you saved.”)

Since we built PostgreSQL from the tarball, rather than using an RPM, we didn’t get some of the conveniences that the RPM provides, like creating the user that the server will run as. So let’s do that!

1[xof@blog ~]$ sudo /usr/sbin/adduser postgres
2[xof@blog ~]$ sudo passwd postgres
3Changing password for user postgres.
4New UNIX password:
5Retype new UNIX password:
6passwd: all authentication tokens updated successfully.

Logging in as our shiny new user, we add the PostgreSQL binaries to our $PATH in the .bash_profile:

1PATH=$PATH:/usr/local/pgsql/bin:$HOME/bin

Well, no time like the present… let’s create a cluster:

1[postgres@blog ~]$ initdb db

We want the locale and encoding to be UTF8, and that happens to be the default:

1The database cluster will be initialized with locale en_US.UTF-8.
2The default database encoding has accordingly been set to UTF8.
3The default text search configuration will be set to "english".

Nota bene that the default authentication is “trust”:

1WARNING: enabling "trust" authentication for local connections
2You can change this by editing pg_hba.conf or using the -A option the
3next time you run initdb.

That’s fine for now (we’ll change it later). As a smoke test, let’s start up the server and make sure all is well:

1[postgres@blog ~]$ pg_ctl -D ~postgres/db -l logfile start

We’re just writing the log to a file ‘logfile’ in the postgres user’s home director, which is not particularly sophisticated log file management. (That’s also something we’ll fix later.)

So, did it work?

1[postgres@blog ~]$ psql postgres
2psql (8.4.1)
3Type "help" for help.
4
5postgres=#

Looks good!

Now, before we start creating new databases, let’s set up a couple of useful defaults.

1[postgres@blog ~]$ psql template1
2psql (8.4.1)
3Type "help" for help.
4
5template1=#

We’ll be using PL/pgSQL functions in the future, so let’s add that language to template1. The template1 database is the template from which new databases are created, so changes here will be propagated to all new databses we create in the future.

1template1=# CREATE LANGUAGE plpgsql;
2CREATE LANGUAGE

We can also add the UUID-OSSP functions:

1template1=# \i /usr/local/pgsql/share/contrib/uuid-ossp.sql
2SET
3CREATE FUNCTION
4CREATE FUNCTION
5CREATE FUNCTION
6CREATE FUNCTION
7CREATE FUNCTION
8CREATE FUNCTION
9CREATE FUNCTION
10CREATE FUNCTION
11CREATE FUNCTION
12CREATE FUNCTION

Great. PostgreSQL has a ton more configuration options, of course, but this will get us started.

One of the other things that you don’t get when you build PostgreSQL from a tarball is an automatic init.d script so that PostgreSQL starts on system boot. Fortunately, the source tarball comes packaged with a suitable startup script:

1[xof@blog ~]$ cd builds/postgresql-8.4.1/contrib/start-scripts
2[xof@blog start-scripts]$ sudo cp linux /etc/rc.d/init.d/postgresql
3[xof@blog start-scripts]$ sudo chmod +x /etc/rc.d/init.d/postgresql

The standard script doesn’t have the right location for the database directory, so we have to edit the appropriate line:

1# Data directory
2PGDATA="/home/postgres/db"

and use chkconfig to add the appropriate symlinks:

1[xof@blog start-scripts]$ sudo /sbin/chkconfig --add postgresql

(We went back to the xof account because the postgres account isn’t a member of wheel, and can’t use sudo, which is just the way it should be.)

So, did that work? Let’s find out!

1[xof@blog ~]$ sudo reboot

(wait wait wait.) Back up!

1[postgres@blog ~]$ psql postgres
2psql (8.4.1)
3Type "help" for help.
4
5postgres=#

Great. Note that the server logs are now being written to the default for the init.d file, which is $PGDATA/serverlogs rather than ~/logfile as before.

Next, in part 3, we’ll install Python 2.6, Apache, mod_wsgi and psycopg2.