More Info:
Instance addresses can be public IP or private IP. Public IP means that the instance isaccessible through the public internet. In contrast, instances using only private IP arenot accessible through the public internet, but are accessible through a Virtual PrivateCloudRisk Level
MediumAddress
Reliability, SecurityCompliance Standards
- CIS GCP
- CIS GCP 2.0.0
Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
To allow public (external) access to a Cloud SQL for PostgreSQL instance in GCP using the Console, you need to add a public IP (you can keep the private IP as well if needed).
Step-by-step in GCP Console
-
Go to Cloud SQL
- In the Google Cloud Console, go to:
Navigation menu → Databases → SQL - Click on your PostgreSQL instance.
- In the Google Cloud Console, go to:
-
Open the Connections settings
- In the instance page, click the Edit button at the top.
- In the left or central tabs, find and click Connections.
-
Enable Public IP
- Scroll to the Connectivity or IP addresses section.
- Under Public IP, check Assign a public IP address (or Add network / Add public IP depending on UI version).
- The Console will show that a public IP will be assigned on save.
-
Configure Authorized Networks (Firewall for DB)
- Still under Public IP, find Authorized networks.
- Click Add Network.
- Enter:
- Name: A label for the client/network (e.g.,
office-networkordev-laptop). - Network: The IP or CIDR that should be allowed (e.g.,
203.0.113.10/32for a single IP).
- Name: A label for the client/network (e.g.,
- Repeat for all client IPs that need access.
- Avoid
0.0.0.0/0unless this is a controlled test environment and you fully understand the risk.
-
Save changes
- Scroll down and click Save.
- Wait for the instance to finish updating (status changes back to RUNNABLE).
-
Get the public IP and connect
- In the instance Overview page, under Connect to this instance or Instance IP addresses, note the Public IP address.
- Use this host in your PostgreSQL client connection string:
- Host: the public IP
- Port: default
5432unless you changed it - User/password / database as configured.
-
(Optional) Keep or remove Private IP
- If you no longer want private-only access:
- Go back to Edit → Connections.
- Under Private IP, uncheck / remove private IP assignment (if your design allows).
- Save again.
- If you no longer want private-only access:
Using CLI
Using CLI
To remediate “PostgreSQL Instance IP Assignment Set To Private” in GCP (i.e., enable a public IPv4 address) via gcloud, do the following:
Confirm it only has
Notes:
You should now see an entry like:
This overwrites the existing list with the one you provide.
These commands will change the instance from “private-only” to having a public IPv4 endpoint while retaining the private IP if it was already configured.
1. Make sure you have the right project and auth
2. Check current IP configuration of the instance
PRIVATE and no PRIMARY / PUBLIC IPv4.3. (Optional but recommended) Define authorized networks
Decide which client IPs should be allowed to connect to the public IP.Example: allow only one office IP and one VPN IP:4. Enable public IPv4 on the instance
Usegcloud sql instances patch with --assign-ip. You can optionally add authorized networks at the same time.--assign-ipadds a public IPv4 address (keeps private IP if already enabled).- If you don’t want to set authorized networks now, just omit the flag and add them later.
5. Verify that a public IP was assigned
6. (Optional) Adjust or add authorized networks later
To update the allowed IP ranges:These commands will change the instance from “private-only” to having a public IPv4 endpoint while retaining the private IP if it was already configured.
Using Python
Using Python
Below is how to change a Cloud SQL for PostgreSQL instance so it has a public IP (in addition to or instead of private IP) using Python.
1. Prerequisites
- Cloud SQL Admin API must be enabled:
-
Authentication:
- Use a service account with the
roles/cloudsql.adminrole. - Set
GOOGLE_APPLICATION_CREDENTIALSto point to its JSON key, or run this from an environment with default credentials (e.g., Cloud Shell, Cloud Run with attached service account).
- Use a service account with the
- Install libraries:
2. Python code to add a public IP
This example:- Adds a public IP to an existing PostgreSQL instance.
- Keeps existing private IP settings intact (if any).
- Optionally restricts access with
authorizedNetworks.
PROJECT_IDwith your project IDINSTANCE_NAMEwith your Cloud SQL instance nameNETWORK_CIDRwith your allowed CIDR (or removeauthorizedNetworksblock if not needed)
3. Notes / Variations
-
Keep both private and public IP:
The code above leaves any existingprivateNetworkandpscConfiguntouched; it just enablesipv4Enabled. That gives you dual (private + public) connectivity. -
Remove public IP later (if needed):
Set
ip_config["ipv4Enabled"] = Falseand patch again. -
No network restriction:
If you omit
authorizedNetworks, any IP can attempt to connect (still needs DB auth). For better security, always defineauthorizedNetworks.
Using Terraform
Using Terraform
private_network plus ipv4_enabled = false) can force replacement of the instance, which may cause downtime; review the terraform plan carefully before applying.For verification, terraform plan should show the google_sql_database_instance gaining settings.ip_configuration.private_network and ipv4_enabled = false, and (for an existing public-only instance) it may show the instance being replaced along with creation of the networking/peering resources if they are new.
