Why I can't connect to the PostgreSQL from my webserver on SiteGround?

73 views
Skip to first unread message

Daniele Fois

unread,
Nov 27, 2019, 12:45:58 PM11/27/19
to Google Cloud SQL discuss

I have a Google Cloud PostgreSQL instance. I can connect to it by several local IP addresses, after having whitelisted them in the connections tab on GCP. I developed my application and now, when I try to connect the instance from production webserver, it says:

Fatal error: Uncaught PDOException: SQLSTATE[08006] [7] could not connect to server: Connection timed out Is the server running on host "34.65.215.xx" and accepting TCP/IP connections on port 5432? 

the PHP I'm using is:

$db2 = new PDO('pgsql:host=34.65.215.xx;port=5432;dbname=xxxxx', 'postgres', 'xxxxxx');

I wrote to my webserver support and they answered me by opening the 5432 port in output on the 34.65.215.xx IP. After that I still can't connect to the instance.

The strace says:

0.000120 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 4
0.000043 setsockopt(4, SOL_TCP, TCP_NODELAY, [1], 4) = 0
0.000031 fcntl(4, F_SETFL, O_RDONLY|O_NONBLOCK) = 0
0.000024 fcntl(4, F_SETFD, FD_CLOEXEC) = 0
0.000027 connect(4, {sa_family=AF_INET, sin_port=htons(5432), sin_addr=inet_addr("34.65.215.xx")}, 16) = -1 EINPROGRESS (Operation now in progress)
0.000112 poll([{fd=4, events=POLLOUT|POLLERR}], 1, 30000) = 1 ([{fd=4, revents=POLLOUT|POLLERR|POLLHUP}])
15.270417 getsockopt(4, SOL_SOCKET, SO_ERROR, [110], [4]) = 0
0.000103 close(4) = 0

I just don't know what to do next...

Can someone help to understand?

Braulio Baron Camargo

unread,
Nov 27, 2019, 6:55:01 PM11/27/19
to Google Cloud SQL discuss
Hi Daniele,

I assume that you are using public IP connections [1]. You can grant any application access to a Cloud SQL instance by authorizing the public IP addresses that the application uses to connect.

Have you added the IP address of your application? If not, please follow this steps (Described in [1]):

1. Determine the IP address of your application.
2. Authorize your application's IP address to connect to your Cloud SQL Instance [2].
3. You can find the IP address assigned to your instance in its Instance details page. This is the value you need for your application's 
connection string.

If your application is dynamically assigned IP address, then you need to use  Cloud SQL Proxy[3].

Please let me know if this clarifies your inquiries.

Reply all
Reply to author
Forward
0 new messages