0

I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function. If I use

$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")

than the error is

Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...

But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.

If I use

$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")

command, the error is

pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out

I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?

coder-coder
  • 323
  • 4
  • 13
  • How remote is 'remote'? The second form looks like it's trying to connect but that the remote server is either off-line or is behind a firewall that's preventing a connection. It's unusual to find a database server accessible on the public internet. – Tangentially Perpendicular Jan 15 '21 at 21:44
  • Actually, I connected to this server via SSH from PHPStorm (test of the connection was successful). "server.address.ru" is written here instead of what I wrote in the "host" field when I created an SSH tunnel. Currently it isn't offline (I can connect to it via PuTTY). – coder-coder Jan 15 '21 at 21:53
  • 1
    That may be your problem, then. You need an SSH tunnel to connect. Connecting direct is blocked, as I would have expected. As to your next question (How to set up an SSH tunnel?) [this question](https://stackoverflow.com/questions/464317/connect-to-a-mysql-server-over-ssh-in-php) might help. – Tangentially Perpendicular Jan 15 '21 at 21:58

1 Answers1

0

Thanks to @TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer. What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):

  1. Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
  2. Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
  3. In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());

The connection is set. For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.

coder-coder
  • 323
  • 4
  • 13