2

When trying to get a psql shell (not using iam user) I am receiving:

> gcloud alpha sql connect pg-instance --database mydb --user myuser --project my-project
Starting Cloud SQL Proxy: [/Users/me/google-cloud-sdk/bin/cloud_sql_proxy -instances my-project:us-central1:pg-instance=tcp:9470 -credential_file /Users/me/.config/gcloud/legacy_credentials/me@me.com/adc.json]]
2022/03/15 14:47:59 Rlimits for file descriptors set to {Current = 8500, Max = 9223372036854775807}
2022/03/15 14:47:59 using credential file for authentication; path="/Users/me/.config/gcloud/legacy_credentials/me@me.com/adc.json"
2022/03/15 14:48:00 Listening on 127.0.0.1:9470 for my-project:us-central1:pg-instance
2022/03/15 14:48:00 Ready for new connections
Connecting to database with SQL user [myuser].Password:
psql: error: connection to server at "127.0.0.1", port 9470 failed: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
  • Just tried the same against my pg instance and was able to connect. I'd try the usual things: update to latest gcloud and latest proxy component. – enocom Mar 16 '22 at 17:35
  • It will be easier for the community to provide an accurate answer if you edit your question adding adding the information from the "Overview" section in the "about connection options" page mentioned in my answer? Like: How did you connect, authorize and authenticate? – Alex Mar 21 '22 at 21:14

2 Answers2

0

I had the same error message when connecting to Postgres(Cloud Sql) using a service account. In my setup I did run cloud_sql_proxy inside docker container.

In order to make it work I had to add extra configuration defined in step #9 https://cloud.google.com/sql/docs/sqlserver/connect-docker#connect-client

docker run -d \
  -v <PATH_TO_KEY_FILE>:/config \
  -p 127.0.0.1:5432:5432\
  gcr.io/cloudsql-docker/gce-proxy:1.33.1 /cloud_sql_proxy \
  -instances=<INSTANCE_CONNECTION_NAME>=tcp:0.0.0.0:5432 -credential_file=/config

The missing bits were: host ip on port mapping and 0.0.0.0: in cloud_sql_proxy command

Ildar Galikov
  • 431
  • 5
  • 17
-2

There are a few things I would like to point out. The best starting point for me would be the About connection options page; both the Overview and the Before you begin sections are very helpful to get the full idea of the process and how to properly configure the user. But the most important part is the Connection Options, for the message connection to server at "127.0.0.1" I’m guessing it is a private IP, but please make sure this section is covered before starting to debug.

In your case, the logs are saying there was an error in the connection to the server… I used the Troubleshoot guide that includes the Diagnose issues link to get to the Debug connection issues page that has a lot of useful information on how to debug any connectivity issue.

Generally, connection issues fall into one of the following three areas:

  • Connecting - are you able to reach your instance over the network?
  • Authorizing - are you authorized to connect to the instance?
  • Authenticating - does the database accept your database credentials?

Each of those can be further broken down into different paths for investigation.

Once determining the connection method, there are different questions that will help to guide you through the possible troubleshooting paths.

If using these guides doesn’t get you a solution, please make sure to update your answer with the results, steps, and information followed to provide further help. This would be a good example, as it has the same log error, and this other question shows that there are a few different troubleshooting paths for this specific log message, plus they have useful information for you.

Alex
  • 778
  • 1
  • 15