PostgreSQL & PHP Tutorials - Installing PostgreSQL

PostgreSQL »  Installing PostgreSQL
PostgreSQL »  Starting Out »  Installing PostgreSQL

Posted By Chris Smith Posted on 07 Feb 2006, 05:12 AM
This is a quick guide to installing PostgreSQL on a Linux based system. We need to get this up and running before we can write PHP scripts to talk to the database.    We need to install PostgreSQL before we can install PHP (since we need the PostgreSQL header files).

This is a little more complicated than installing Apache.

The best bet is to install from packages since this will make things easier, both now and for upgrades.

If you decide to install from source code, you'll need to create a user and compile the source.

Create a PostgreSQL user (you'll need root access). This is who owns the system. I use the "postgres" user and group, you can use something different but it's best to keep it related to the package.

See your system documentation on how to do this (hint - man useradd). Give the user a home directory of /usr/local/pgsql as this is where PostgreSQL defaults to when it installs.

Once all that is done, untar the source:

$ tar -zxf postgresql-<version>.tar.gz (if you got the gzipp'ed version);
or
$ tar -xjf postgresql-<version>.tar.bz2

Jump into the directory:

$ cd postgresql-<version>

(replace <version> with the version you downloaded from the website).

Again, configure has a lot of help:

$ ./configure --help

We'll just install into /usr/local/pgsql and leave the rest as-is:

$ ./configure --prefix=/usr/local/pgsql

Next we have to make it (this will take a while, it takes longer than Apache):

$ make

You will get a line:
All of PostgreSQL successfully made. Ready to install.

when everything is finished.

You'll need to log in as root to do the rest:

$ su -

Go back to the postgresql directory and do:

# make install

PostgreSQL doesn't allow root to start up the server for security reasons, so next you'll have to change the owner of the files:

# chown -R postgres:postgres /usr/local/pgsql

Change to the postgres user:

# su - postgres

Then, change to /usr/local/pgsql:

$ cd /usr/local/pgsql

We need to initialize a database cluster:

$ bin/initdb -D ./data

This will create a /usr/local/pgsql/data directory and initialize it ready for startup and use.

As usual, you can view options by using the --help switch:

$ bin/initdb --help

Then you can start:

$ bin/pg_ctl -D ./data -l data/logfile start

Now that postgresql is started, we need to create a database and another user before we go any further.

You should use a separate database for each of our projects, it's not essential though - it just makes things a little cleaner and easier to understand.

You should also use separate users for each database. This keeps everything separate and 'project a' won't be able to modify any of 'project b's data.

To create a new user in postgresql, it's pretty simple:

$ /usr/local/pgsql/bin/createuser

and follow the prompts - pretty simple :)

The new user should not be able to create new databases or create new users.


To create a database, it's a little different.

$ /usr/local/pgsql/bin/createdb --owner=username databasename

We have to set the owner of the database when we create it, otherwise the 'postgres' user owns it and then we have to grant access to allow our new user to access it.


Now that that's all done, log out of the postgres user, so we're back as root. In the postgres directory, the contrib/start-scripts/ directory has a few system start up scripts.

Follow the instructions in the relevant one to install a start-up script so postgres will start when your system does.

More detailed installation instructions are in the INSTALL file or by going to the PostgreSQL website http://www.postgresql.org/docs/ and going to the right version for you.
Avg Rating: 4
Vote Count: 62


              

  New Reply


Ive been searching for such resource for a long time. Its great. LurkingUkulele 16 Jun 2006 Reply

Ive been searching for such resource for a long time. Its great. DelightfullyWistful 16 Jun 2006 Reply

Youve got very useful site. It really helped me. Thanks. MurkyDeliriousSunrise 19 Jun 2006 Reply

Thanks for giving such a useful information. MyQuaintLight 19 Jun 2006 Reply

Youve got a great site. I found it very useful. Persey Mc Bright 21 Jun 2006 Reply

I found this resource very usefull. Thanks. Kim Sligter 21 Jun 2006 Reply

Great project. I found it very useful. LurkingUkulele 22 Jun 2006 Reply

Great project. I found it very useful. GloomilyMagicalEchidna 23 Jun 2006 Reply

would you pls try postgresql installation in windows article. Mohamed 30 Dec 2006 Reply
`-- There is a windows installer, very simple to install it now.

See http://pgfoundry.org/projects/pginstaller/
Chris Smith 16 Jun 2007 Reply

What about upgrades? This doesn't mention if I need to change any apache or php settings nor does it mention what might happen to my existing databases. jta 17 Apr 2007 Reply
`-- Upgrading postgresql is a separate issue to installing.

Upgrades need more attention and I'll write about them separately.
Chris Smith 16 Jun 2007 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 8 + 7 =
Comment