Sunday, August 17, 2014

Installing PostgreSQL without root Access

In this post I will explain how to install PostgreSQL database without linux root account. There are several ways to install PostgreSQL as outlined in this site. In this post we're going to use installing from source-code approach. To do this you don't need root access.

Follow these simple steps and you will be having installing in 10 minutes.

I'm using PostgreSQL 9.3.5, on CentOS 6 platform.

Download source code using this site
1. Extract the source  code       
tar -xvf postgresql-9.3.5.tar.gz

2. Goto extracted directory and configure using following command
./configure --preifx=/home/guest/postgresql-9.3.5/
Here prefix is the installation directory.

3. Build the code
make

4. Install the code
make install


That's it !. You have installed PostgreSQL. Now let try check the installed database.
To make working with the database more easier, set following environment variables in .bash_profile
export PGDATA=/home/guest/postgresql-9.3.5/data
 
Now add the PostgreSQL bin directory to the path,
export PATH=$PATH:/home/guest/postgresql-9.3.5/bin

Now, we can start the server using following command,
pg_ctl -l logfilename start

This will start the server and direct the output to file named "logfilename" in current directory.

To stop the server you can use
pg_ctl -l logfilename stop
 
You can check all the available options using,
pg_ctl --help


Now let's try to login to the database and execute some queries. To do this first we need to initiate the database. To initiate the database execute
initdb

After this command ran successfully, you can login to the database using psql program.

To login,
psql postgres

In above command postgres is the database. This was created in initdb setup.

Now we can execute queries.  Check following query result for test,

select * from pg_database;

This will list all the databases.
You can exit from psql using following command.
\q