Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cloud functions can't connect to Postgres Cloud Sql

Hello!

I followed every step in the tutorial "Connect to Cloud SQL for PostgreSQL from Cloud Functions" for a Private IP.

https://cloud.google.com/sql/docs/postgres/connect-instance-cloud-functions

At the end of the tutorial, when I click the URL under the function details, I get the following error:

PeytonCox_0-1695696470088.png

PeytonCox_1-1695696523173.png

I've redone the tutorial several times, following each step exactly, and I keep getting the error.

What should I do? I'm not sure why it's trying to connect to a public IP when I set it for a private IP.

Happy to share any more information that would be relevant.

0 1 1,616
1 REPLY 1

The error message cloudsqlconnectorerror cannot connect to instance, public Ip address not found indicates that your Cloud Function is trying to connect to your Cloud SQL instance using a public IP address, but your instance does not have a public IP address. This is likely because your Cloud SQL instance is configured to only allow connections from private IP addresses.

To fix this, ensure your Cloud Function is configured to connect to your Cloud SQL instance using a private IP address. Follow these steps:

Use Serverless VPC Access:
  1. Create a Serverless VPC Access connector in the same region as your Cloud Function and Cloud SQL instance.
  2. Configure your Cloud Function to use this connector.
Connection Configuration:
  1. In your Cloud Function code, use the instance connection name to connect to the Cloud SQL instance. The instance connection name is in the format PROJECT_ID:REGION:INSTANCE_ID.
  2. Use the @Google-cloud/cloud-sql-connector library for connecting to the Cloud SQL instance.
Additional Troubleshooting Tips:
  1. Ensure that your Cloud SQL instance is running and that you can connect to it from another application.
  2. Ensure that your Cloud Function has the correct permissions to connect to your Cloud SQL instance.
  3. Try redeploying your Cloud Function.
  4. Check the logs of your Cloud Function for any errors.

If you are still having trouble connecting to your Cloud SQL instance, please contact Google Cloud support for assistance.