Install Postgresql in Ubuntu 12.04 LTS

Hi all,

Its common to all that we need to install database in our machine now or then due to re-installation of OS or any other reasons whatever that may be. I too use to install most of the time due to some reason or other.

Today I am installing Postgres or Postgresql database on my new installation of Ubuntu 12.04 LTS. I wanted to document this so that it will help others too and mostly that I can refer to whenever I need to do the same again.

Type following command in the terminal to install postgresql (9.1 is the default version that Ubuntu 12.04 provides) :

manohar@manohar-desktop:~$ sudo apt-get install postgresql

Now edit the /etc/postgresql/9.1/main/pg_hba.conf file and add/change the following line :

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

Now edit the /etc/postgresql/9.1/main/postgresql.conf file and change :

#listen_addresses = 'localhost'


listen_addresses = '*'

so that the TCP/IP connection for everyone is enabled which is by default disabled. Or you can change the “*” to any IP range suitable to you.

And change the following to enable password encryption :

#password_encryption = on


password_encryption = on

Now create an user as superuser for your current user (if you like to) :

manohar@manohar-desktop:~$ sudo -u postgres createuser --superuser $USER

To create a database with a user that have full rights on the database, use the following command:

manohar@manohar-desktop:~$sudo -u postgres createuser -D -A -P manohar
manohar@manohar-desktop:~$sudo -u postgres createdb -O manohar mydatabase

The first command line creates the user with no database creation rights (-D), with no add user rights (-A) and will prompt you for entering a password (-P). The second command line create the database mydatabase with manohar as owner.

If you have created current user as superuser, you can create the database(named simulator) using following commands :

manohar@manohar-desktop:~$ createdb simulator

To use the just created database(i.e. simulator), use the following command :

manohar@manohar-desktop:~$ psql simulator
psql (9.1.8)
Type "help" for help.


Now you can give database related commands in the new database prompt.

Here is some test commands for example :

simulator=# create table test(name text);
simulator=# \d
List of relations
Schema | Name | Type  |  Owner
public | test | table | manohar
(1 row)

simulator=# \d test
Table "public.test"
Column | Type | Modifiers
name   | text |




Hope this helps you all.

Manohar Bhattarai (मनोहर भट्टराई)
Personal Website :
Personal Blog :

I am Manohar Bhattarai, a vivid java programmer. I am a keen Linux user. I currently boot Ubuntu Linux. I am a Free/Libre Open Source Software enthusiast.

Posted in Postgres Tagged with: , , , , , , , , , , , , , , , , , , , ,
3 comments on “Install Postgresql in Ubuntu 12.04 LTS
  1. chand says:

    This is the easiest way to install postgresql in ubuntu 12.04.
    Thank you

  2. victor says:

    Not so far I have found new cool tool to work with PostgreSQL on ubuntu — Valentina Studio.

2 Pings/Trackbacks for "Install Postgresql in Ubuntu 12.04 LTS"
  1. […] sql commands. So, I will write how to execute .sql file from command line in postgresql today. See this howto to see how to install postgresql in […]

  2. […] The above error is normally due to wrong password or you have not set the password for that user already. You can change the password for the user to login to the database again. You can check how to install and do the settings for postgresql in Ubuntu 12.04 here. […]

Leave a Reply