PostgreSQL with PHP in Ubuntu: Step-by-Step How To!

This document is for latest version of PostgreSQL: 8.4, but it should work in any newer versions, as long as you change the commands appropriately, replacing the version mentioned with correct version 🙂


Install PostgreSQL:

sudo apt-get install postgresql

Install GUI Administration application:

sudo apt-get install pgadmin3

Install PHP based Web Administration site (like phpMyAdmin for MySQL database):

sudo apt-get install phppgadmin

I found this helpful, because it installed all dependant packages required to access postGRE database from PHP 😉


Configure so that you can access via localhost:

gksudo gedit /etc/postgresql/8.4/main/postgresql.conf 

It witll open the file for editing, Add following line at the end of the file:

listen_addresses = 'localhost'

Save and close the file. Open another file for editing:

gksuso gedit /etc/postgresql/8.4/main/pg_hba.conf

Replace “local all all ident sameuser” with:

local   all         all                               md5

Change Password for root user

In PostGRE, root user is “postgres” which by default, does not have any password. Enter following line in terminal to set a password for it:

sudo -u postgres psql template1
ALTER USER postgres with encrypted password 'your_password';

Create a new User & a new Database

sudo -u postgres createuser -d -R -P new_username
sudo -u postgres createdb -O new_username new_database_name

This will create a new user, with username “new_username” and create a new database “new_database_name” and set “new_username” it’s owner.

Configure phpPgAdmin

I assume you already installed phpPgAdmin by:

sudo apt-get install phppgadmin

Then, configure Apache:

gksudo gedit /etc/apache2/apache2.conf

Add following line at the end of the file:

Include /etc/phppgadmin/apache.conf

All done! Restart to reflect changes…

sudo /etc/init.d/apache2 restart
sudo /etc/init.d/postgresql-8.4 restart

Access phpPgAdmin

type http://localhost/phppgadmin in your browser & log in by the username you just created (new_username)

Use GUI Administration application

Run following command in terminal:


Access from Terminal:



15 thoughts on “PostgreSQL with PHP in Ubuntu: Step-by-Step How To!”

  1. Hey man…. I am still working through this, but I’m alll about mySQL… currently converting to psql and changing some company projects. You said ALL dependencies for php to work with psql… So I dont need to install php5_pgsql for the php? or do I? Apache2 web server->postgres->php website is what will be setup. All the php is done except My original trial I had issues that some php methods and functions for pg wouldnt work correctly.
    much Thanks

  2. hey! I’m having a strange problem. our postgresql is installed on ubuntu, as we are using openstreetmap database. now i’ve to install the phppgadmin on windows 7 and have to access the postgresql on ubuntu. i know we’ve gotta make some changes in config.ini file but i m just confused as where to place the phppgadmin, because when i installed the postgresql on windows, i placed both the postgresql and phppgadmin in wamp server but now situation is different 😡

  3. Thank you – I did it, it worked. One small note for those who access phpPgAdmin remotely and get 403 error:
    you have to modify /etc/phppgadmin/apache.conf and comment out “deny from all”, plus uncomment “allow from all”. Alternatively, include your own IP mask on the list. Worked for me – any issues I should be aware of?

  4. Thanks buddy, great tut ……For those running postgresql9.1 you might need to Replace “host all all ident sameuser” with: “host all all md5”.

  5. I get this error 😦

    The Alias directive in /etc/phppgadmin/apache.conf at line 1 will probably never match because it overlaps an earlier Alias.

    should anyone fix this ??? Install on Ubuntu 12.04

  6. Thanks for this.

    It almost worked 100% for me except I had to allow my ip range in the etc/phppgadmin/apache.conf file in order to access the apache web page

Comments are closed.