Friday, August 04, 2006

Following on from my last post about PostgreSQL, i've found a couple of things were 'off' with the setup, so this is a quick re-write and update... on with the show!

So, if the last guide worked, what's different with this set-up...

* We now enable and use the default 'postgres' user account to administrate our database server (saves creating a new one)!
* We also fix a couple of issues with the networking set-up.
* We now use the latest build of PostgreSQL - v8.1.
* This set-up is less of a 'hack', than the last guide (i've been reading since then).

Before we move on, this guide was tested on (and intended for) the current stable build of Ubuntu (5.04 - Breezy Badger), but it should also work fine on any other build of Ubuntu/Debian (6.06 - Dapper Drake etc).

First off, PostgreSQL 8.1 isn't in the main repositories in Breezy, you'll need to have backports enabled to get hold of the latest packages. Once you've done that, let's move on.

Right, now let's install the database server. At the command-line, enter the following (Or you can do all this in Synaptic - just search for and install the packages listed in the commands):

> sudo apt-get install postgresql-8.1 postgresql-client-8.1
> sudo apt-get install pgadmin3 pgadmin3-data

This installs the database server, and the pgAdmin administration application (If you don't really get on with the pgAdmin GUI, there is an alternative in the form of phpPgAdmin - a web-based administration interface. A quick 'How To' on getting this up will be coming shortly! ;) ).

Now we need to reset the password for the 'postgres' admin account for the server, so we can use this for all of the system administration tasks. Type the following at the command-line (substitute in the password you want to use for your administrator account):

> sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD '*password*';
template1=# \q

Then, from here on in we can use pgAdmin to run the database server. To get a menu entry for pgAdmin do the following...

> sudo gedit /usr/share/applications/pgadmin.desktop

Then paste the following into the file:

[Desktop Entry] Comment= PostgreSQL Administrator III
Name=pgAdmin III
Encoding=UTF-8
Exec=pgadmin3
Terminal=false
Comment[en_GB]=PostgreSQL Administrator III
Icon=/usr/share/pixmaps/pgadmin3.xpm
Type=Application
Categories=GNOME;Application;Database;System;
Name[en_GB]=pgAdmin III

Then save the file and exit gedit. You should find the launcher in the System Tools section of the Applications menu. Alternatively, you could just type 'pgadmin3' at the shell. The wizards to connect to the database should be pretty simple to figure out.

Finally, we need to open up the server so that we can access and use it remotely - unless you only want to access the database on the local machine (The guidelines here are for opening up your server on a secure LAN - if you are not on a secure LAN you may want to look into adding SSL authentication before proceeding with these steps).

To do this, first, we need to edit the postgresql.conf file:

> sudo gedit /etc/postgresql/8.1/main/postgresql.conf

Now, to edit a couple of lines in the 'Connections and Authentication' section...

Change the line:

#listen_addresses = 'localhost'

to

listen_addresses = '*'

and also change the line:

#password_encryption = on

to

password_encryption = on

Then save the file and close gedit.

Now for the final step, we must define who can access the server. This is all done using the pg_hba.conf file.

> sudo gedit /etc/postgresql/8.1/main/pg_hba.conf

Now add the following lines to the file:

# Allow any user on the local system to connect to any database under
# any user name using Unix-domain sockets (the default for local
# connections).
#
# Database administrative login by UNIX sockets
local all all trust

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all md5

# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5

# Connections for all PCs on the subnet
#
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host all all [ip address] [subnet mask] md5

and in the last line, add in your subnet mask (i.e. 255.255.255.0) and the IP address of the machine that you would like to access your server (i.e. 138.250.192.115). However, if you would like to enable access to a range of IP addresses, just substitute the last number for a zero and all machines within that range will be allowed access (i.e. 138.250.192.0 would allow all machines with an IP address 138.250.192.x to use the database server).

That's it, now all you have to do is restart the server and all should be working!
> sudo /etc/init.d/postgresql-8.1 restart

No comments: