12.2. Installing the Database

First thing to do is to install the PostgreSQL database server[141]:

# yum install -y postgresql-server

Then start the database, which will take a few seconds to initialize for the first time:

# service postgresql start

Next, create a user called asterisk which we will use to connect to and manage the database. Run the following commands:

# su - postgres
$ createuser -P
Enter name of user to add: asterisk
Enter password for new user: 
Enter it again: 
Shall the new user be allowed to create databases? (y/n) y
Shall the new user be allowed to create more new users? (y/n) n
CREATE USER

By default, PostgreSQL does not listen on the TCP/IP connection which Asterisk will be using. We need to modify the /var/lib/pgsql/data/postgresql.conf file in order to allow Asterisk to make IP connections to the database. To do this, simply remove the comment from the beginning of the tcpip_socket and port parameters. Be sure to change the tcpip_socket option from false to true.

tcpip_socket = true
max_connections = 100
        # note: increasing max_connections costs about 500 bytes of shared
        # memory per connection slot, in addition to costs from shared_buffers
        # and max_locks_per_transaction.
#superuser_reserved_connections = 2
port = 5432

Now, edit the /var/lib/pgsql/data/pg_hba.conf file in order to allow the asterisk user we created above to connect to the PostgreSQL server over the TCP/IP socket. At the end of the file, add the following line:

host    all     asterisk        127.0.0.1       255.255.255.255         md5
local   all     asterisk                                                trust

Now we can create the database we will use throughout this chapter. We're going to create a database called asterisk and set the owner to our asterisk user.

$ createdb --owner=asterisk asterisk
CREATE DATABASE

Restart the PostgreSQL server after exiting from the postgres user back to root.

$ exit
# service postgresql restart

We can verify our connection to the PostgreSQL server via TCP/IP like so:

# psql -h 127.0.0.1 -U asterisk
Password:
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

		 asterisk=>

Double check your configuration as discussed above if you get the following error, which means connections via the TCP/IP socket is not allowed.

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


[141] On a large, busy system you will want to install this on a completely separate box from your Asterisk system.