Installing PostgreSQL from sources is a very easy and grateful task. It allow you to customize a lot of stuffs in your database server (I really appreciate the option of choose where to install my softwares, but this is not all).
You can get the PostgreSQL sources at http://www.postgresql.org/ftp/source/. I got the sources for the version 8.3.7, since it’s the latest stable version, so far.
Before installing the Postgres, first I have to install the readline library (I am using a Ubuntu 8.10 installed some days ago, maybe in a brand new installation you need to install some other packages):
# apt-get install libreadline5-dev
Then, extract the .tar.gz or .tar.bz2 file and inside the created directory run:
NOTE: The directory after the prefix argument (/opt/postgresql.org/…….) is where your postgres server will be installed, change it to wherever you want.
# ./configure --prefix=/opt/postgresql.org/postgresql-server/postgresql-8.3.7
And then:
# make
If everything goes well you’ll see the message :
All of PostgreSQL successfully made. Ready to install.
Now run:
# make install
Again, if everything goes well, the following message will be shown:
PostgreSQL installation complete.
So far, so good. Easy as said, but you still have some steps to do:
Create a new group and a new user to run the server:
# groupadd postgres
# useradd -g postgres -d /home/postgres -s /bin/bash -m postgres
Now create a directory to put the database files. You can choose any directory that you want. I use to put my variable size files on /var/local, so I create this one:
# mkdir /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data -p
# chown -R postgres:postgres /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data
Log as this user:
# su - postgres
edit the .bashrc file and append this to the end:
PATH=/opt/postgresql.org/postgresql-server/postgresql-8.3.7/bin:$PATH
export PATH
Run the following command:
$ source .bashrc
Now we are ready to create the database files.
$ initdb -D /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data
And then start the server (I’m using a tempory log file, you can choose other if you wish):
$ pg_ctl -D /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data -l /tmp/postgres.log start
See the log file to check if the server are ok.
Now stop the server:
$ pg_ctl -D /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data stop
The last thing: To start the server in system initialization copy the file linux from contrib/start-scripts/ to /etc/init.d/postgres. (Look in the sources directory)
Remember of change the permission of the file to 755:
# chmod 755 /etc/init.d/postgres
Open the file /etc/init.d/postgres on an editor and change the line with prefix to wherever you install the postgres (The same directory used in the prefix attribute from the configure command).
I had to change the following lines:
prefix=/opt/postgresql.org/postgresql-server/postgresql-8.3.7
PGDATA="/var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data"
Save the file and exit.
Now run:
# update-rc.d -f postgres defaults
To ensure that everything is ok run:
# /etc/init.d/postgres start
Check the file /var/local/postgresql.org/postgresql-server/postgresql-8.3.7/data/serverlog to verify that the server is up and ok.
Good job! You now have a brand new Postgres 8.3 server up and running!
One Tip: A lot of useful information can be found on file INSTALL inside the source directory. If you have some trouble installing Postgres take a look at this file.
One more tip: One great tool to work with Postgres Server is PgAdmin. You can find it in the Ubuntu Add/Remove Applications.