Algum dia, em algum momento, você vai necessitar fazer com que o Asterisk grave as informações das ligações (tabela CDR) em um banco de dados, tipo 
Postgres. Este trabalho foi baseado no 
Elastix 2.4.0 e que, se seguir corretamente, o sucesso é garantido.
Configurando o Postgres
Instalando os pacotes:
 yum install mc postgresql91 postgresql91-server
Criando a base de dados, iniciando o serviço e habilitando auto-início no boot:
 service postgresql-9.1 initdb
 service postgresql-9.1 start
 chkconfig postgresql-9.1 on
Atribuindo a senha no usuário 
postgres do PostgreSQL:
 su - postgres
 psql
postgres=# 
\password postgres
-> Digite nova senha:
-> Digite-a novamente:
Definindo 
password:
postgres=# 
CREATE USER asterisk WITH PASSWORD 'passw0rd';
-> CREATE ROLE
Criando o banco de dados:
postgres=# 
CREATE DATABASE asterisk OWNER asterisk;
-> CREATE DATABASE
postgres=# 
\q
Criando a tabela para ser usada pelo 
asterisk (cdr):
 psql -d asterisk
CREATE TABLE cdr (
calldate timestamp with time zone NOT NULL default now(),
clid varchar(80) NOT NULL default '',
src varchar(80) NOT NULL default '',
dst varchar(80) NOT NULL default '',
dcontext varchar(80) NOT NULL default '',
channel varchar(80) NOT NULL default '',
dstchannel varchar(80) NOT NULL default '',
lastapp varchar(80) NOT NULL default '',
lastdata varchar(80) NOT NULL default '',
duration bigint NOT NULL default '0',
billsec bigint NOT NULL default '0',
disposition varchar(45) NOT NULL default '',
amaflags bigint NOT NULL default '0',
accountcode varchar(20) NOT NULL default '',
uniqueid varchar(32) NOT NULL default '',
userfield varchar(255) NOT NULL default ''
);
-> CREATE TABLE
Definindo os privilégios para o usuário criado:
asterisk=# 
GRANT ALL PRIVILEGES ON cdr TO asterisk;
-> GRANT
postgres=# 
\q:
exit
Liberando acesso remoto ao Postgres:
 mcedit /var/lib/pgsql/9.1/data/postgresql.conf
Substituir linhas: 
#listen_addresses = 'localhost'
Para:
listen_addresses = '*'
E: 
#port = 5432
Para:
port = 5432
Liberando direitos ao acesso remoto:
 cp /var/lib/pgsql/9.1/data/pg_hba.conf /var/lib/pgsql/9.1/data/pg_hba.conf.bak
 mcedit /var/lib/pgsql/9.1/data/pg_hba.conf
Alterar para que fique parecido com abaixo:
  # TYPE  DATABASE     USER        ADDRESS          METHOD
  # "local" is for Unix domain socket connections only
  local   all          all                           md5
  # IPv4 local connections:
  host    all          all      127.0.0.1/32         md5
  # IPv6 local connections:
  host    all          all        ::1/128            md5
  [...]
  host    all          all       0.0.0.0/0           md5
Reiniciando o Postgres:
 /etc/init.d/postgresql-9.1 restart
Instalando o administrador via WEB:
 yum install phpPgAdmin
Permitindo acesso remoto ao administrador WEB:
 mcedit /etc/httpd/conf.d/phpPgAdmin.conf
Substitua a linha: 
Deny from all
Para:
Allow from all
Configurando o acesso ao administrador WEB:
 mcedit /etc/phpPgAdmin/config.inc.php
Substituir:
Linha 18:
→ $conf['servers'][0]['host'] = '';
Para:
$conf['servers'][0]['host'] = 'localhost';
Linha 93:
→ $conf['extra_login_security'] = true;
Para:
$conf['extra_login_security'] = false;
Linha 99:
→ $conf['owned_only'] = false;
Para:
$conf['owned_only'] = true;
Reiniciando o 
httpd:
 service httpd restart
Reiniciando o Postgres:
 service postgresql-9.1 restart
Testando a conexão:
# psql -U postgres
-> Senha para usuário postgres:
-> psql (9.1.14)
-> Digite "help" para ajuda.
->
-> postgres=#
Para acessar o administrador WEB: 
http://ip-address/phpPgAdmin
(Se precisar, o diretório do PostgreSQL é: 
/var/lib/pgsql/9.1/data/)
Configurando o Asterisk
Permitindo carregar o módulo de suporte ao Postgres:
 mcedit /etc/asterisk/modules.conf
Comentar com um ; a linha 78, com o conteúdo: 
noload => cdr_pgsql.so
Para:
;noload => cdr_pgsql.so
Configurando o acesso ao banco Postgres/tabela cdr:
 mcedit /etc/asterisk/cdr_pgsql.conf
Deixar parecido com isso:
[global]
hostname=localhost
port=5432
dbname=asterisk
password=passw0rd
user=asterisk
table=cdr               ; SQL table where CDRs will be inserted
encoding=LATIN9         ; Encoding of logged characters in Asterisk
;timezone=UTC           ; Uncomment if you want datetime fields in UTC/GMT
Reiniciando o Asterisk:
 /etc/init.d/asterisk restart
Testando se o Asterisk reconheceu o Postgres:
 asterisk -vvvvr
-> asterisk*CLI> cdr show status
Deverá aparecer na lista o: 
pgsql
Nenhum comentário foi encontrado.