0

Why doesn't this code work in a model in codeigniter. It works perfectly in "normal" php (with the same variables for the connection).

$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
                or die ("Could not connect to server\n"); 

The following error pops up after some amount of time:

Message: pg_connect(): Unable to connect to PostgreSQL server: could not connect to
server: Connection timed out Is the server running on host "198.xxx.xxx.xxx" and accepting 
TCP/IP connections on port 5432?

The answer to this, is as I know, yes

Øyvind
  • 979
  • 4
  • 17
  • 34
  • Btw, I have tried connecting to multiple databases following various tutorials, it does not work, but I get other errors. I am connecting to this external database to fetch some data from a sister site. – Øyvind Feb 19 '14 at 23:52
  • When you say it works in "normal" PHP, is this ran on the same server as Codeigniter with the errors? – Sam Feb 19 '14 at 23:52
  • No, another server. However, on the CI server, I have the option to use a postgres database, so would it not be fair to assume the server can handle this? Do you have something else in mind? – Øyvind Feb 19 '14 at 23:54
  • Try the "normal" php script on the CI server (but outside of the CI installation). If it doesn't work, then your database server isn't allowing access from the CI server. – Samutz Feb 19 '14 at 23:57
  • I'm thinking that the remote PostgreSQL server you are connecting to has restricted access (possibly only `localhost`, or specific IPs). This is default activity for security reasons. Check [this question](http://stackoverflow.com/questions/6150738/cannot-connect-to-postgres-from-remote-host). – Sam Feb 19 '14 at 23:57
  • @Sam - This is already fixed :) – Øyvind Feb 19 '14 at 23:58
  • @Samutz - No, I can't run the code outside Codeigniter on the same server... But I did set up the database server to allow any IP to connect to it... – Øyvind Feb 20 '14 at 00:14
  • @Øyvind What about a firewall on either server possibly blocking the connection? – Samutz Feb 20 '14 at 00:36
  • I don't think there is a problem on the database server, as I did everything by the book to open for all IPs. How can I find out if there is something blocking it on the CI server? It is a server run by a hosting company, running Cpanel, giving me some limitations. – Øyvind Feb 20 '14 at 00:48
  • _Connection timed out_ is typical of a firewall dropping the network packets. Check with your hosting provider what kind of outbound traffic is allowed. – Daniel Vérité Feb 20 '14 at 01:56

0 Answers0