I've a server with postgresql installed on the postgres user. When I do:
su - postgres
[entering the password]
$dbconnection = pg_connect("host=localhost port=5432 user=postgres password=XXXXXXX ");
What is the current configuration for pg_hba.conf? And did you set a password for the database rol "postgres" ? su - postgres is for the Linux user "postgres", not for the database role "postgres". If you can start psql without entering the password, it looks like pg_hba.conf uses "trust" and not "password" of even better "md5". Or did you create a .pgpass file?