1

Connecting to a PostgreSQL database via a remote IP address, I have been successful via Windows using pgAdmin III, but I get errors whenever I try connecting from my local CentOS 6 Apache web server using the standard php-pgsql library using pg_connect().

Notes:

  1. I am not in control of the remote server, but could inquire about additional info if needed.
  2. My password does contain a special character (although it's not a quote character).

Creds are as follows:

Host (IP): xx.xx.xx.xx
Port: 5432
DBname: sandbox
Username: abc
Password: ***
MaintenanceDB: template1

This is the PHP code I have attempted to run in my local server:

pg_connect("host=xx.xx.xx.xx port=5432 dbname=sandbox user=abc password=***");

I've also tried:

pg_connect("host=xx.xx.xx.xx port=5432 dbname=template1 user=abc password=***");

When I attempt to connect, I receive the following error:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "xx.xx.xx.xx" and accepting TCP/IP connections on port 5432?

I have added/uncommented extension=pgsql.so in the /etc/php.d/ directory and service httpd restart. I've even gone as far as opening my iptables ports as such so there should be no doubt about local firewall ports being blocked:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 5432 -j ACCEPT

Thanks in advance :-)

Jon Lawton
  • 890
  • 3
  • 14
  • 33
  • Are you sure the login credentials are correct? – BenM Jan 10 '15 at 18:53
  • I was able to connect via pgAdmin III, which indicates that the creds are correct AND access is enabled for remote access. I'm not sure how the Maintenance DB fits into the PHP side (OPTION parameter perhaps?) and if there is something else I'm missing. – Jon Lawton Jan 10 '15 at 18:57
  • For me, look like firewall problem. Can you temporary disable firewall on CentOS and try again. – Nebojsa Susic Jan 10 '15 at 19:06
  • 2
    I had the same problem last week, check this one: http://stackoverflow.com/questions/27749691/php-cant-connect-to-postgresql-on-centos-7 – Frank Heikens Jan 10 '15 at 19:34
  • 1
    Note about my duplicate vote: the key is **Permission denied** when attempting a **TCP connection** to postgres. This particular combination on CentOS/Redhat is invariably due to SELinux not configured to allow the HTTP server to issue outbound connections. – Daniel Vérité Jan 10 '15 at 19:34

1 Answers1

0

As per your postgresql connection problem, this solution may work. i just take this answer from php manual and i am not sure about it, so please see here for more http://php.net/manual/en/function.pg-connect.php#38291

You should try to leave the host= and port= parts out of the connection string. This sounds strange, but this is an "option" of Postgre. If you have not activated the TCP/IP port in postgresql.conf then postgresql doesn't accept any incoming requests from an TCP/IP port.

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103