The problem is that you're using persistant conections (pg_pconnect() function).
Each time you request a page which uses this function, it estableshes a new connection to the database and it leaves it open forever, so next time a DB connection is needed, the existent connection can be used.
When you want to shutdown postgresql, it tells you that there are active connections. What I do, is to send a SIGHUP to httpd before shutting down postgresql, like this
killall -HUP httpd
this closes every persistant connection because apache kills all its child proceses, re-reads the configuration and creates new childs, which are not connected to the DB.
About pg_close(), the php site says:
"pg_close() will not close persistent links generated by pg_pconnect()", so it won't work.