I'm trying to connect postgreSQL remotely to PHP on an Apache server. On my local machine I can run the connection without any problem. The point is it's not working remotely.
I am running the same code that ran on another instance of the same cloud service (changing only the IP). No error message is displayed and the code message indicating that the database was not connected does not appear. I am using an Ubuntu 14.04 server. can anybody help me? I can not find the problem!
Checked:
- Postgresql MV configuration by psql over SSH connection and PgAdmin3 remotely. Both working perfectly.
- Setting the password ok.
- Configuration of pg_hba.conf (as indicated in the hired cloud service tutorial)
# IPv4 remote connections (all users and IP addresses): host all all 0.0.0.0/0 md5
-
Postgresql.conf configuration (as indicated in the contracted cloud service tutorial)
listen_addresses = '*'
-
Firewall rules - ok.
-
PHP code (the same code works on the local machine perfectly and worked on another instance of the same cloud service).
<?php $host = "host=xx.xx.xx.xx"; $port = "port=5432"; $dbname = "dbname=nomedabasededados"; $credentials = "user=postgres password=senha"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } $sql = pg_query("INSERT INTO tabela (coluna1) VALUES ('valordacoluna1');"); ?>