PostgreSQL

Q. How do I install PostgreSQL server under Ubuntu Linux?

A. PostgreSQL is an Open Source Software object relational database management system. POSTGRES pioneered many of the object-relational concepts now becoming available in some commercial databases. Traditional relational database management systems (RDBMS) support a data model consisting of a collection of named relations, containing attributes of a specific type. In current commercial systems, possible types include floating point numbers, integers, character strings, money, and dates.

Task: Install PostgreSQL

The best way to install PostgreSQL under Debian or Ubuntu Linux is to use apt-get command
# apt-get install postgresql
OR
$ sudo apt-get install postgresql
Output:

Reading package lists... Done
Building dependency tree... Done
The following extra packages will be installed:
  openssl postgresql-7.4 postgresql-client postgresql-client-7.4 postgresql-client-common postgresql-common ssl-cert
Suggested packages:
  ca-certificates postgresql-doc-7.4
Recommended packages:
  postgresql-plpython-7.4 postgresql-plperl-7.4 postgresql-pltcl-7.4
The following NEW packages will be installed
  openssl postgresql postgresql-7.4 postgresql-client postgresql-client-7.4 postgresql-client-common postgresql-common
  ssl-cert
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 5620kB of archives.
After unpacking 14.3MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get: 1 http://cudlug.cudenver.edu stable/main openssl 0.9.8c-4 [1004kB]
......
........
..
...
Setting up postgresql-7.4 (7.4.16-1) ...
Creating new cluster (configuration: /etc/postgresql/7.4/main, data: /var/lib/postgresql/7.4/main)...
Moving configuration file /var/lib/postgresql/7.4/main/postgresql.conf to /etc/postgresql/7.4/main...
Moving configuration file /var/lib/postgresql/7.4/main/pg_hba.conf to /etc/postgresql/7.4/main...
Moving configuration file /var/lib/postgresql/7.4/main/pg_ident.conf to /etc/postgresql/7.4/main...
Configuring postgresql.conf to use port 5432...
Starting PostgreSQL 7.4 database server: main.

Setting up postgresql-client (7.5.22) ...
Setting up postgresql (7.5.22) ...

Task: Connect to PostgreSQL server

You can login as PostgreSQL superuser, enter command:
$ su - postgres
Now connect to default database called template1:
$ psql template1
Output:

postgres@debian:~$ psql template1
Welcome to psql 7.4.16, the PostgreSQL interactive terminal.

Type:  \\copyright for distribution terms
       \\h for help with SQL commands
       \\? for help on internal slash commands
       \\g or terminate with semicolon to execute query
       \\q to quit

template1=# 

Type \l to list database
Type \q to quit

See also:

Q. When I try to connect remote PostgreSQL, I am reciving an error which read as follows:

psql: could not connect to server: Connection refused
Is the server running on host host.domain.com and accepting
TCP/IP connections on port 5432?

How do I fix this problem? I am using CentOS 4.x version.

A. First make sure PostgreSQL server has been started to remote server.

# /etc/init.d/postgresql start

If it is running and you get above error, you need to add enable TCP/IP support. By default, the PostgreSQL server only allows connections to the database from the local machine or localhost. This is a security feature.

Step # 1: Allow remote IP address to access PostgreSQL

You need to open file called /var/lib/pgsql/data/pg_hba.conf. Login as postgres user using su command:
$ su - postgres
$ vi /var/lib/pgsql/data/pg_hba.conf

Now append following line. Let us say you would like to give access to 192.168.1.0/24 network:
host all all 192.168.1.0 255.255.255.0 trust
Please replace 192.168.1.0 and 255.255.255.0 to reflect the actual network IP address range of the clients system in your own network.

Save close the file.

Step # 2: Allow communication over TCP/IP

You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf
$ vi /var/lib/pgsql/data/postgresql.conf
Now bind and open TCP/IP port by setting tcpip_socket to true:
tcpip_socket = true

Save and close the file.

Step # 3: Restart PostgreSQL server

Restart the PostgreSQL server with the following command
# /etc/init.d/postgresql restart

This will open default port 5432.

Step # 4: Test your setup

Use psql command from client system as follows:
psql -h PostgreSQL-IP-ADDRESS -U USERNAME -d DATABASENAME

Connect to remote server by IP address 192.168.1.5 and login using vivek user to connect to sales database, use:
$ psql -h 192.168.1.5 -U vivek -d sales
Where,

  • -h 192.168.1.5 : Specifies the host name of the machine or IP address (192.168.1.5) on which the server is running.
  • -U vivek : Connect to the database as the vivek username instead of the default. You must have account and permission to connect as vivek user.
  • -d sales : Specifies the name of the database (sales) to connect to.

See also:

=> How do I add a user to PostgreSQL database server?