Getting started with PostgreSQL

PostgreSQL database server installation and configuration in debian

Introduction
PostgreSQL is a fully featured object-relational database management system. It supports a large part of the SQL standard and is designed to be extensible by users in many aspects. Some of the features are: ACID transactions, foreign keys, views, sequences, subqueries, triggers, user-defined types and functions, outer joins, multiversion concurrency control. Graphical user interfaces and bindings for many programming languages
are available as well.

Postgresql Database Installation in Debian

If you want to install the PostgreSQL base system as well as a textual clientrun the following command from your shell

    apt-get update
    apt-get install postgresql-8.3 postgresql-client-8.3 postgresql-contrib-8.3 postgresql-common

This will install a lot of package dependencies as well. The version of PostgreSQL installed should be 8.3.7 on a debian lenny (5.0.1 in my case) system. If you want to check the official documentation click here. http://www.postgresql.org/docs/8.3/interactive/index.html

NOTE: After installation if you will try to start postgresql then it may not start if you have not created database cluster.
it is simple to initialize database cluster [It is being used to store databases].

#pg_createcluster 8.3 main --start That will display some information on your screen. once it completes successfully, you will able to login to postgresql database using default way to login as mentioned below.

The only (default) way to log into your newly created database system is to log as root, then to log as user postgres, by issuing


    #su
    #su postgres
    #psql template1
    #template1=# \q
    #exit


However, you would probably like to be able to login as your unix user, or any other user you choose. There are several ways to set up these accesses, so let's decomposeAlowing local users to login is done by editing the /etc/postgresql/pg_hba.conf file (a complete documentation of this file is available here). There, you have to retrieve a series of lines like the following ones


    local all postgres ident sameuser
    #
    # All other connections by UNIX sockets
    local all all ident sameuser
    #
    # All IPv4 connections from localhost
    host all all 127.0.0.1 255.255.255.255 md5
    # All IPv6 localhost connections
    host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff ident sameuser
    host all all ::ffff:127.0.0.1/128 ident sameuser
    #
    # reject all other connection attempts
    host all all 0.0.0.0 0.0.0.0 reject


This file has limited accesses but we will come back here later.Now what we have told PostgreSQL is that all users on the local machine should be able to access all databases, using their UNIX login and passwords. You need to restart your database to take new changes effect

Restart postgresql using the following command

    #/etc/init.d/postgresql restart

Now the problem is that you have authorized users to log into the database, but they will only be able to log in if they also exist in the database. So you need to create accounts with their usernames. This is done by issuing the createuser command as user postgres as follows


    #su postgres
    #createuser firstuser
    #exit


The createuser command will ask you if the user should be able to create databases or create new users. Answer truly.

Now you have a new user in the database, and you should be able to connect as this user (let's call him firstuser) like this:


    #su - firstuser
    #firstuser@localhost:~$ psql -W template1
    #template1=# \q
    #exit


The -W parameter is to ask for a password prompt. You should enter your system password when asked.
So that's it, we got the first part of this installation running. Now let's prepare this for the web.

If some of your web scripts are going to need a connection to the database, you will need to grant them some access via a new user, reserved for this web usage.

Let's first create this new user (as user postgres). Let's call him webuser.


    #su postgres
    #createuser webuser
    #exit


Now you want to give him access to one or several databases (let's say you will let him access the databases which name is web). To do this, you need to edit once more the pg_hba.conf file. Retrieve the first line below and write the second one just beneath the first.


    host all all 127.0.0.1 255.255.255.255 md5
    host web webuser 127.0.0.1 255.255.255.255 md5


Would you like to give this user access from any computer on the sub-network 192.168.0.xxx, you would have to add the following line

    host web webuser 192.168.1.1 255.255.255.0 md5

You have to grant him access to the host as it will probably use the TCP/IP protocol to get a connection to the database, and that's what host is for. But as you have given him an md5 authentication type, you need to give him a password as well. In order to do this, you need to connect to the database and issue a special command, all as postgres user:


    #su postgres
    #postgres@localhost:~$ psql template1
    #template1=# alter user webuser password 'some_password';
    #ALTER USER
    #template1=# \q
    #exit


Now the user should have a nice 'some_password' password that you can use after restarting PostgreSQL to make it remember your changes to pg_hba.conf.

Restart postgresql using the following command

    #/etc/init.d/postgresql restart

And you should be able to create his own database if you gave him the permission

    #createdb -h localhost -U webuser -W web

And connect to this newly-created database using the following command


    #psql -h localhost -U webuser -W web

As you might have noticed, we use -h localhost here. This is to force the PostgreSQL client to use TCP/IP instead of UNIX sockets. If you don't want is, you need to add a new line into the "local" category of pg_hba.conf for the user webuser to connect via sockets.The line would be something like

local web webuser md5

Using SQL

The first important command you might want to know is

#psql -h localhost -U webuser -W -l

This will give you a list of available databases.

Now connect to the PostgreSQL database using the psql client, and let's create a small table, just after a few tests


    web=> SELECT version();
    web=> SELECT current_date;
    web=> SELECT 2+2;
    web=> \h
    web=> \d
    web=> \dt
    web=> CREATE TABLE weather(
    web=> city varchar(80),
    web=> temp_lo int, -- low temperature
    web=> temp_hi int, -- high temperature
    web=> prcp real, -- precipitation
    web=> date date
    web=> );
    web=> INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    web=> VALUES ('London', 12, 13, 0.0, '2005-05-29');
    web=> INSERT INTO weather (date, city, temp_hi, temp_lo)
    web=> VALUES ('2005-05-19', 'Miltons', 10, 25);
    web=> SELECT city, temp_lo as 'Lowest Temperature', temp_hi as 'Highest Temperature' FROM weather;
    web=> SELECT city FROM weather WHERE temp_lo < 30;
    web=> SELECT max(temp_lo) FROM weather;
    web=> UPDATE weather SET temp_lo = 18 WHERE city = 'London';
    web=> SELECT * FROM weather;
    web=> DELETE FROM weather WHERE prcp IS NULL;
    web=> SELECT * FROM weather;
    web=> \q

Now you've had a nice overview of several SQL statements and how to use them in PostgreSQL.

Later, you might want to create a table with a numerical sequence as an index. This link will help you get through this step which I find counter-intuitive and user-unfriendly at will. You might as well know of a shortcut. If you want to create a default auto-incremental key that will be used by your table, you need to define (for every table) a SEQUENCE. This id done, for the weather table above, like this (with the help of user comments here):


    web=> CREATE SEQUENCE my_table_pk_sequence;
    web=> CREATE TABLE weather(
    web=> id int not null default nextval('my_table_pk_sequence') PRIMARY KEY,
    web=> city varchar(80),
    web=> temp_lo int, -- low temperature
    web=> temp_hi int, -- high temperature
    web=> prcp real, -- precipitation
    web=> date date
    web=> );

And then insert elements as before, ignoring the id field


    web=> INSERT INTO weather (date, city, temp_hi, temp_lo)
    web=> VALUES ('2009-06-20', 'London', 14, 27);
    web=> SELECT * FROM weather;

This should have inserted an auto-incremented id.

Back to top