PostgreSQL Installation in Linux(Python)


PostgreSQL is a powerful, open source object-relational database system. It runs on all major operating systems, including Linux, UNIX and Windows. It has native programming interfaces for C/C++, Java, .Net, Perl, Python, Ruby, Tcl, ODBC.

Install PostgreSQL in your Linux Machine

If you are using Ubuntu you can use the apt command

  • apt-get install postgresql

and if you are using Pardus you can use pisi

  • pisi it postgresql

Create a Database and grant permissions to Users

Add a Linux/UNIX user called odol

  • adduser odol
  • passwd odol

This’ll create the user odol. This is not a required step. This is only for testing purpose. You can use any existing user also.

Becoming a superuser and connect to database server

  • su – postgres

This user will get created automatically when we install PostgreSQL.

  • psql template1

If you are not becoming a superuser, you can do the following

  • psql -d template1 -U postgres

Add a user called odol and Create a database called mydb and Grant Permissions

By the execution of last command, you’ll enter the postgres shell. As the db is template1, the prompt will be template=#

  • CREATE USER odol WITH PASSWORD ‘myPassword’;
  • \q

Enter as odol and test

  • su – odol
  • psql -d mydb -U odol

This will let you get into the shell for mydb database. In which you can create table and do other database activities and queries.

Using PostgreSQL with Django

In order to use Python with PostgreSQl we need an adapter. Psycopg is the most popular PostgreSQL adapter for the Python programming language.
Install the following requirements first

  • apt-get install python-dev libpq-dev

We can get pyscopg using pip or easy_install

  • pip install pyscopg2

Common Errors while installing pyscopg2

The most common error(the one I came across) while installing pyscopg2:

  • Error: pg_config executable not found.

Make sure you have installed python-dev and libpq-dev in your system. The step is already mentioned above.

Admin for PostgreSQL(Graphical)

pgAdmin is an Open Source administration and development platform for PostgreSQL. It provide the graphical interface. And also have an editor in which you can create SQL query and execute it.


About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on March 15, 2012, in Python and tagged , , , . Bookmark the permalink. 2 Comments.

  1. Great ! Your blog looks professional and i like the contents in it


  2. @Sathish thanks man…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: