Using PostgreSQL on ubuntu 12.04 LTS

PostgreSQL is a powerful object-relational database management system and has  bindings for many programming languages such as C, C++, Python, Java, PHP, Ruby…

Before you use it, you need to install it, ofcourse. To install it run following commands on your terminal:

apt-get install postgresql postgresql-contrib

You can also download it from ftp://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.gz and
After you have obtained the file, unpack it:

gunzip postgresql-9.2.4.tar.gz
tar xf postgresql-9.2.4.tar

For the rest of the installation procedure, you need to change into the directory, which you created while unpacking it.

Creating a Database
To create a database, you need to login as a postgres user.

gurpinder@gurpinder:~$ sudo -u postgres -i

To create a new database, issue following command on your terminal:

postgres@gurpinder:~$ createdb testdb
postgres@gurpinder:~$ psql testdb

You should see output similar to the following:

psql (9.1.9) 
Type "help" for help. 
testdb=#

Creating Tables
To create a table in your test database called “student”, issue the following command:

testdb=#  CREATE TABLE student (student_id int, first_name varchar, last_name varchar);

Now if you want  insert a record into the table, use this:

testdb=# INSERT INTO student VALUES (2, 'Gurpinder', 'Chahal');

Now if you want to see the contents of table, issue this command:

testdb=# SELECT * FROM student;

This would produce output similar to the following:

testdb=# SELECT * FROM student; 
 student_id | first_name | last_name 
------------+------------+----------- 
          2 | Gurpinder  | Chahal 
(1 row)

To exit the psql shell, issue this command:
\q

If you do not want to use your data anymore, remove it by:

$ dropdb testdb

For more knowlegde of PostgreSQL look here: http://www.postgresql.org/docs/9.2/interactive/index.html

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s