res_config_pgsql: Fix a memory leak and use RAII_VAR for cleanup when practical

Review: https://reviewboard.asterisk.org/r/3141/
........

Merged revisions 406360 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 406361 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 406389 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: http://svn.asterisk.org/svn/asterisk/trunk@406399 f38db490-d61c-443f-a65b-d21fe96a405b